JavaScript JavaScript not writing variable as number (loop) [duplicate] writing javascript variable in html,writing javascript in variable

Your event handler function has an enduring reference to i, not a copy of its value, as you've discovered.

To prevent that, have the function close over something else that won't change:

for(var i=1;i<9;i++){
    document.getElementById('element'+i).onclick=makeHandler(i);
}

function makeHandler(index) {
    return function() {
        theFunc(index);
    };
}

makeHandler creates a function that closes over index, which is a copy of the value of i, and so doesn't change as the loop continues. Each event handler gets its own index.

That said, creating a bunch of event handler functions that are effectively identical usually means you can redesign a bit and use just one handler function. In this case, for instance, you could do this:

for(var i=1;i<9;i++){
    document.getElementById('element'+i).onclick=theHandler;
}

function theHandler() {
    func(parseInt(this.id.replace(/\D/g, ''));
}

...which grabs the value to use from the id of the element.

Another approach is delegation, where you actually hook the click event on an ancestor element (one that all of these elementX's have in common), and then when the click occurs, look at event.target and its ancestors to see what you should do.

Answer:1

TJ Crowder's answer is the best way around your problem. This "problem" you're experiencing in your closure is by design in many languages, and is referred to as scope.

Here's a good explanation of different scopes in JavaScript (including closures) and how to use them. http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/

Answer:2

I have a leaflet map and I want to assign colors to each "feature." The number of features can be different depending on changing geojson data. I am guessing there is some algorithm to do this. Here ...

I have a leaflet map and I want to assign colors to each "feature." The number of features can be different depending on changing geojson data. I am guessing there is some algorithm to do this. Here ...

  I need to color all the check boxes that are checked to green and unchecked one's to red. <style> input[type=checkbox]+span{ color:red; } input[type=checkbox][checked]+span{ ...

  I need to color all the check boxes that are checked to green and unchecked one's to red. <style> input[type=checkbox]+span{ color:red; } input[type=checkbox][checked]+span{ ...

Please let me know if this is a duplicate post, but I was not able to find a solution to my issue. Its gotta be something simple... I'm trying to use javascript to build a form. The html for the form ...

Please let me know if this is a duplicate post, but I was not able to find a solution to my issue. Its gotta be something simple... I'm trying to use javascript to build a form. The html for the form ...

  1. line breaks from textarea
  2. line breaks form
  3. line breaks from word
  4. line break form html
  5. line break contact form 7
  6. line break pdf form
  7. line break permit form
  8. remove line break form
  9. line break online form
  10. jotform line break
  11. line break poetic form

I'm trying to send a an array to my sender from my receiver but when I try it it will turn it into a string. I have the code: messageBus = window.castReceiverManager.getCastMessageBus(namespace, ...

I'm trying to send a an array to my sender from my receiver but when I try it it will turn it into a string. I have the code: messageBus = window.castReceiverManager.getCastMessageBus(namespace, ...

  1. chromecast send message
  2. chromecast send message from receiver
  3. chromecast custom receiver send message