0
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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Creating input DOM is inside a loop and it seems that when I code nameInput.onchange = function(){ editAddressHidden(nameInput.id) }; it returns the last namInput.id found in the table.
0

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.

1 Comment

Always good to explain why it works, rather than just what works. Otherwise the OP isn't going to learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.