Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am hiding a paragraph on button_click and showing again on the click of the same button using jquery.
But when I click on the button nothing happens not even error is displayed. I have checked using inspect element in the browser. There is no error. Here is my code.
C#
<body>
<%--<script type="text/javascript" language="javascript" src="Script/Hideshow.js"></script>--%>
  <%--<script src="Script/jquery-2.1.1.js" type="text/javascript"></script>--%>
   
    <script src="Script/jquery-2.1.3.min.js" type="text/javascript"></script>
  <script type="text/javascript">
     
      $('#hide_show').click(function () {
          alert("hello");
          var value = $('#hide_show').attr('value');
          $('#message').toggle('fast');
          if (value == 'Hide') {
              $('#hide_show').attr('value', 'Show');

          }
          else if (value == 'Show') {
              $('#hide_show').attr('value', 'Hide');
          }
      });
  </script>
    <form id="form1" runat="server">
    <div>
        <input id="hide_show" type="button" value="Hide"  />
    <p id="message">This is paragraph </p>
    </div>
    </form>
</body>

Please tell me what is wrong in this code.
Posted
Comments
Kornfeld Eliyahu Peter 18-Mar-15 10:11am    
Check this: http://jsfiddle.net/uLnd1ctm/
The only change is that I wrapped the binding of click in document-ready...

You try to fetch hide_show before it's created.

To be sure that you only execute the JavaScript code when all DOM objects are created, there are two options:

  1. You can put the script at the end of your body tag.
  2. You can execute the code in a document.ready event handler, like this:
    JavaScript
    $(function() {
        // the code here is executed when the DOM is ready
        $('#hide_show').click(function () {
            alert("hello");
            var value = $('#hide_show').attr('value');
            $('#message').toggle('fast');
            if (value == 'Hide') {
                $('#hide_show').attr('value', 'Show');
            }
            else if (value == 'Show') {
                $('#hide_show').attr('value', 'Hide');
            }
        });
    });

 
Share this answer
 
You have placed the script code before the HTML elements. hide_show is not there yet when you declare #hide_show.
You should either put the script code inside $( document ).ready() or after all the HTML elements.
XML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
      $('#hide_show').click(function () {
          alert("hello");
          var value = $('#hide_show').attr('value');
          $('#message').toggle('fast');
          if (value == 'Hide') {
              $('#hide_show').attr('value', 'Show');

          }
          else if (value == 'Show') {
              $('#hide_show').attr('value', 'Hide');
          }
      });
});
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="hide_show" type="button" value="Hide"  />
    <p id="message">This is paragraph </p>
    </div>
    </form>
</body>
</html>

$( document ).ready()[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900