3

My jQuery is not working, i don't know what am i doing wrong.

The button is supposed to hide the paragraph and bring it back when i press it.
Have put the html and toggle.js code.
I am referencing the jquery library from a folder called js.

HTML

<DOCTYPE = html>
<html lang = "en">
<head>
    <meta charset = "utf-8">
    <script src=  "js/jquery-1.6.2.js"></script>
    <script src = "js/toggle.js"></script>

    <title>Jquery Example</title>
</head>
<body>
    <input type = "button" value = "Hide" id = "toggle_message"/>
    <p id = "message">You can see this paragraph</p>
</body>
</html>

TOGGLE.JS

$('#toggle_message').click(function() {

  var value = $('#toggle_message').attr('value');
  $('#message').toggle('fast');

  if (value == 'Hide') {
    $('#toggle_message').attr('value', 'Show');
  } else if (value == 'Show') {
    $('#toggle_message').attr('value','Hide');
  }
});

6 Answers 6

5

Try wrapping your code with $(function() {}); It likely can't access the DOM yet. e.g

$(function() {

    $('#toggle_message').click(function() {

      var value = $('#toggle_message').attr('value');
      $('#message').toggle('fast');

      if (value == 'Hide') {
        $('#toggle_message').attr('value', 'Show');
      } else if (value == 'Show') {
        $('#toggle_message').attr('value','Hide');
      }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Check this.

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    </head>
    <body>
        <input type = "button" value = "Hide" id="toggle_message"/>
        <p id = "message">You can see this paragraph</p>
    </body>
    <script type="text/javascript">
        $(document).ready( function () {
            $('#toggle_message').click(function() {        
                var value = $('#toggle_message').attr('value');
                $('#message').toggle('fast');

                if (value == 'Hide') {
                    $('#toggle_message').attr('value', 'Show');
                } else if (value == 'Show') {
                    $('#toggle_message').attr('value','Hide');
                }
            });
        });
    </script>
</html>

1 Comment

I was missing "$(document).ready( function () " on top of the script.Thanks guys.
0

Your click handler attaches to the DOM even before your elements are present in the DOM.

Enclose the code in your toggle.js like this:

$(function(){
  // your code here

});

Alternatively, you can place your script tag after the elements actually attach to the DOM, like:

<DOCTYPE = html>
<html lang = "en">
<head>
    <meta charset = "utf-8">
    <script src=  "js/jquery-1.6.2.js"></script>


    <title>Jquery Example</title>
</head>
<body>
    <input type = "button" value = "Hide" id = "toggle_message"/>
    <p id = "message">You can see this paragraph</p>

    <script src = "js/toggle.js"></script>
</body>
</html>

Comments

0

Try this.

<body>
    <input type = "button" value = "Hide" id = "toggle_message"/>
    <p id = "message">You can see this paragraph</p>
</body>


<script>
    $(function(){
        $('#toggle_message').click(function() {

            var value = $('#toggle_message').attr('value');
            $('#message').toggle('fast');

            if (value == 'Hide') {
              $('#toggle_message').attr('value', 'Show');
            } else if (value == 'Show') {
              $('#toggle_message').attr('value','Hide');
            }

        });
    });
</script>

Comments

0

Your code works fine as you can see here.

The reason why it dosen't in your case is beacuse the DOM is loaded after the javascript code runs so you need to wrap your code in document ready

$( document ).ready()

or a self invoking anonymous function or better yet include the script just before the closing body tag so it runs after the DOM is loaded

Comments

0

Your code will work fine, just put your toggle.js code in a function like
$( document ).ready() { }

Your toggle.js should be-

$( document ).ready() {
 $('#toggle_message').click(function() {

  var value = $('#toggle_message').attr('value');
  $('#message').toggle('fast');

  if (value == 'Hide') {
    $('#toggle_message').attr('value', 'Show');
  } else if (value == 'Show') {
    $('#toggle_message').attr('value','Hide');
  }
 });
}

and always include Javascript files at the bottom of the page i,e before </body> tag.

Comments

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.