javascript - How to return a value from an anonymous inner function in JS? -
My page allows users to add and remove input boxes to enter search terms. The function that allows them to add them, returns a value (as a way to keep track of a calculated variable amount, I want to limit the field to five.)
I have a button here I call the function on click of:
& lt; Input type = "button" value = "+" id = "add" onclic = "calculation = pairfilled (calculated);" /> As you can see, the value of the count has been returned to the script.
To add the following input field is relevant JS. (There is also an internal function to remove the input field in the app function)
starts at // 1, because always there is at least one input box, Var calculation = 1; AddField function (count) {// only if the limit does not reach the limit: if (Count & lt; = 4) {// Increase count by 1, Warning for demo purposes: Calculation ++; Warning (count); // To delete it, create a div to keep the new input and a new button: var divv = document.createElement ("div"); Divv.setAttribute ("id", "div" + count); Divv.setAttribute ("class", "divvv") inputContainer.appendChild (divv); Var id = "tag" + count; var newField = document.createElement ("Input"); newField.setAttribute ('type', 'text'); newField.setAttribute ('class', 'field'); NewField.setAttribute ('id', id); divv.appendChild (Newfield); Var deleteCont = document.createElement ("div"); Var divId = "cont" + count; DeleteCont.setAttribute ("id", "cont" + count); DeleteCont.setAttribute ("class", "remontant"); Divv.appendChild (deleteCont); Var removeId = "remove" + count; Var remove = document.createElement ("input"); Remove.setAttribute ("type", "button"); Remove.setAttribute ("value", "-"); remove.setAttribute ('class', 'remove'); Remove.setAttribute ('id', removeId); // This part is a problem, when the delete button is clicked, it will remove the // relevant device, but I can not find any way to reduce the count after that (i.e., allowing the user to add again Give 5 fields after reaching the range and after removing anything. Basically this allows the user to add 3 (total now at 4). If they remove all three, then add them to 4. But it will only give only one permission. Because 4 was removed even after counting 4. Click to remove = function () {var element = document.getElementById ("inputContainer"); divv.parentNode RemoveChild (divv); // I tried to do something like this: // count = count - 1; // But obviously the value that is received from the addField function is // out of this internal function.}; DeleteCont AppendChild (remove); return count;}} Hopefully you can understand this issue, if I spell it
Demo:
Because your The a ddField function has a variable named count , not using the count variable in the global code by calling count can go. In addition, when your internal function is reference count , it will have a stored value in the closing which is not shared in many invitations of addField to remove this correct object Good for, but bad to keep counting I suggest: JS
starts with // 1, because always there is at least one input box, Var calculation = 1; Var idGen = 1; // Input ID is used to ensure the unique work addField () {// do not count passing now; Var id = idGen ++; // Each field becomes a separate ID; Count ++; // Calculation increases ... button. Click on = function () {... count--; } ... return; // no longer counting; } By the time you do not have to do the ID continuously, separating your ID from your counting will create a design that is easy to write and maintain.
Comments
Post a Comment