Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

In my javascript code var x=1.When some event occured, 'var x' will be incremented by 1. The updated 'x' value will be used for some other events. But the variable x value is always 1. It is because the javascript code is compiled when the page is loaded.Now, how to use the updated value in the same page for other events? Here is my code

 var x=1;<br>
$(button).click(function(e){ <br>
         e.preventDefault();<br>
             x=x+1;   <br>        
     });<br>
//Now,when I would like to do some other event like..<br>

$("#newdiv"+x).click(function() {<br>
        alert("cliked");<br>
});<br>

I have some division with id="newdiv1" , id="newdiv2" and so on. How should differentiate the click on different divisions based on 'x' value.

share|improve this question
1  
add the code, otherwise, it doesn't make sense what you are asking – giannisf Jul 2 at 12:49

Inside the event do x=x+1; or x++

Like this

var x=1; // x is initialized to 1;
....someEvent(){
   x++;
}
share|improve this answer

Where is this code?

$("#newdiv"+x).click(function() {
alert("cliked");
});

The issue here is that you're just using "x" to identify a div but then the function attached is generic. If you'd like to find out which div was clicked then use something like this:

$("#newdiv"+x).click(function(event) {
var source = event.target || event.srcElement;
console.log(source);
alert($(this).attr("id"));
};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.