[RESOLVED]2nd alert doesn't fire

 

My 2nd  alert doesn’t fire when I click the checkbox. Can anyone help?

<script type ="text/javascript">
        jQuery(document).ready(function () {
            alert(jQuery(‘p’).text());
            $(‘#CheckBox1′).prop(‘checked’, ‘true’);

        });

 
        $(‘#CheckBox1′).click(
                function () {

           alert(‘Hello’);
           });
    </script>

You need to put the code to attach the click event handler to checkbox inside the document.ready block of jquery

You can try with the below code

 <script type ="text/javascript">
  jQuery(document).ready(function() {
          alert(jQuery('p').text());
        $('#CheckBox1').prop('checked', 'true');
      //Attach click event handler to checkbox
      $('#CheckBox1').click(function() {
          alert('Hello');
      });
});

    </script>

Complete Code

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <script type ="text/javascript">
jQuery(document).ready(function() {
      alert(jQuery('p').text());
        $('#CheckBox1').prop('checked', 'true');
//Attach click event handler to checkbox $('#CheckBox1').click(function() { alert('Hello'); }); }); </script> </head> <body> <p>Test</p> <input type="checkbox" id="CheckBox1" />CheckBox1 </body> </html>

Check your checkbox control id, and make sure with qualified name, it seems like your using ASP.Net checkbox if so then use the below code

jQuery(document).ready(function () {

$('#<%=CheckBox1.ClientID%>').prop('checked', 'true');

});
$('#<%=CheckBox1.ClientID%>').click(
function () {

alert('Hello');
});

Thanks for the help A2H… it worked. Now I added the function to the code and it doesn’t work. Can you help?

jQuery(document).ready(function() {
          alert(jQuery(‘p’).text());
        $(‘#CheckBox1′).prop(‘checked’, ‘true’);
      //Attach click event handler to checkbox
      $(‘#CheckBox1′).click(function() {
          alert(‘Hello’);
      });
                $(function () {
                     var myArray = ["one", "two", "three", "four", "five"];
                      $.each(myArray, function (index, value) {
                          alert(‘in each’ + ‘ ‘ + value);
          });
            });

davelewicki

Now I added the function to the code

Can you provide the updated code

You missed to close the document.ready block and thats the reason its not working.

Please try with the below updated code

  <script type ="text/javascript">
       
 jQuery(document).ready(function () {
            alert(jQuery('p').text());
            $('#CheckBox1').prop('checked', 'true');
            //Attach click event handler to checkbox
            $('#CheckBox1').click(function () {
                alert('Hello');
            });
            $(function () {
                var myArray = ["one", "two", "three", "four", "five"];
                $.each(myArray, function (index, value) {
                    alert('in each' + ' ' + value);
                });
            });
        });

  </script>

Complete Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <script type ="text/javascript">
       
 jQuery(document).ready(function () {
            alert(jQuery('p').text());
            $('#CheckBox1').prop('checked', 'true');
            //Attach click event handler to checkbox
            $('#CheckBox1').click(function () {
                alert('Hello');
            });
            $(function () {
                var myArray = ["one", "two", "three", "four", "five"];
                $.each(myArray, function (index, value) {
                    alert('in each' + ' ' + value);
                });
            });
        });

  </script>
</head>
<body>
  <p>Test</p>
<input type="checkbox" id="CheckBox1" />CheckBox1
</body>
</html>

Leave a Reply