简体   繁体   中英

html/javascript: how to onclick on non-existent element in html

How to call a function when I click over an 'option' (HTML select > option) when that element is created in javascript and not exists in the html ? here I'm not using jquery or ajax, is just javascript

in my html I only have:

<fieldset>
    <legend>List...</legend>

    <select id="select1" size="9" style="width:150px">
        <option>Select a person</option>
    </select>
</fieldset>
  • more options (persons) are created in another function

Use the onchange event

Without using jquery

document.body.onchange = function(e) {
    if (e.target.tagName=='SELECT'){
        SelectedOption(e.target);
    }
}
function SelectedOption(option){
    alert(option.value);
    alert(option.id);
}

Another example from the select control

<select onchange="javascript:SelectedOption(event.target)" > ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM