简体   繁体   中英

onchange of DOM in Javascript not working

var nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.className = "loop_input";
nameInput.id = "nameAddress"+validLoadedEmails;
nameInput.value = data[0];
nameInput.onchange = editAddressHidden(validLoadedEmails);

it seems that when creating the element. It will already go to the editAddressHidden function and not when there's a change.

Given your current code:

nameInput.onchange = editAddressHidden(validLoadedEmails);

You can think of this as saying "handle the onchange event of nameInput with whatever's returned from my editAddressHidden function". But that's not right (in this instance), as you want to handle the onchange event with the editAddressHidden function itself.

So wrap it up in an anonymous function:

nameInput.onchange = function(){ editAddressHidden(validLoadedEmails) };

Now every time the onchange event fires, it will invoke your function, and this will in turn call the editAddressHidden function.

Use this

nameInput.onchange = function(){ editAddressHidden(validLoadedEmails) };

Because you call editAddressHidden function and assign returned value as input's change handler, that is not what you wanted.

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