0

I am using variable to refer an html div element but it doesnt working. Can anyone tell me how to fix?

html tags:

<body>
<button id="btn1" type="button">ClickMe</button><br />
<div id="div1" style="background-color:red;"></div>
</body>

jquery code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var dv1 = $("#div1");

$(document).ready(function(){
  $("#btn1").click(function(){
    dv1.fadeToggle("slow");
  });
});
</script>
2
  • Please explain exactly what you are expecting to have happen. Without that, I'm only guessing. Commented Apr 17, 2014 at 16:10
  • Felix has answered my question. Commented Apr 17, 2014 at 16:56

1 Answer 1

1

Try to put your variable dv1 inside DOM ready handler:

$(document).ready(function(){
    var dv1 = $("#div1");
    $("#btn1").click(function(){
        dv1.fadeToggle("slow");
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it working! Can i make dv1 to global reference variable?

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.