I'm somehow confused. What is the best way to use one variable as global in jQuery - just to use it two click function ? Here is the code :
HTML :
<div id = "div1">DIV # 1</div>
<div id = "div2">DIV # 2</div>
<input type = "button" value = "Click to see whick div you've chosen."/>
<h1></h1>
jQuery :
$(document).ready(function() {
var answer;
$("#div1").click(function(event) {
window.answer = "FIRST";
});
$("div2").click(function(event) {
window.answer = "SECOND";
});
$("button").click(function() {
$("h1").html(window.answer);
});
})(jQuery);
CSS :
#div1 {
background-color: red;
height: 50px;
}
#div2 {
background-color: blue;
height: 50px;
}
I need to SAVE text to the answer variable. Thanks in advance.
div2instead of#div2, so the second div doesn't have a click event. Your code, fixedwindowis already global (and really unrelated to the declared var)(and#. If the minor syntax errors were not in the fiddle, OP might not have asked the question at all, because the code would've worked.