1

This is doing my head in, I have googled the life out of it, but below is my code, very simple, but I do not get the click event triggered when unchecking a checkbox??

$('#filterStarDiv #hotelFilterForm #Star0').click( function() {
alert('Checkbox 0 clicked');});

<div id="starsRemoved" style="display:none">No Stars Removed</div>
<div id="filterStarDiv">
<h6>Click on the star ratings below to add or remove hotels in that category</h6>
<form:form id="hotelFilterForm" action="" acceptCharset="UTF-8">
    <input type="checkbox" id="Star0"  name="Star0" value="0 Star" checked="checked" /> 0 Star<br />
    <input type="checkbox" id="Star1"  name="Star1" value="1 Star" checked="checked" /> 1 Star<br />
    <input type="checkbox" id="Star2"  name="Star2" value="2 Star" checked="checked" /> 2 Star<br />
    <input type="checkbox" id="Star3"  name="Star3" value="3 Star" checked="checked" /> 3 Star<br />
    <input type="checkbox" id="Star4"  name="Star4" value="4 Star" checked="checked" /> 4 Star<br />
    <input type="checkbox" id="Star5"  name="Star5" value="5 Star" checked="checked" /> 5 Star+<br />
</form:form>
</div>

Any help, even abuse if I am missing something stupid, would be appreciated.

4
  • Ids are unique, you don't have to address them as a dependency like this: #filterStarDiv #hotelFilterForm #Star0. Just #Star0 will be enough. Commented Jul 21, 2011 at 8:37
  • What is: <form:form and </form:form> ? Also input elements in a form need to be put inside a <div> or a <p>. You can't put them directly inside the <form> tag. Commented Jul 21, 2011 at 8:38
  • Seems to work fine to me: jsfiddle.net/Maga2 (I fixed the form tag of course.) Commented Jul 21, 2011 at 8:40
  • api.jquery.com/change Commented Jul 21, 2011 at 8:43

5 Answers 5

2

You can try something like this :

$("input[type=checkbox]").click( function() {
    if ($(this).is(':checked')){
        alert('check');
    }
    else{
        alert('uncheck');
    }
});

You can try it on this Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Try this for all checkbox. Use Id if you want to do for any specific checkbox

$(':checkbox').click(function(){
    var el = $(this);
    if(!el.is(':checked')){
       alert ('unchecked');
       //if you want to trigger handler or do something. do it here
       el.triggerHandler('click');
   }
})

Comments

0

Try this for your javascript:

$().ready(function() {
    $(':checkbox').click(function() { alert($(this).attr('name')); });
});

1 Comment

Using onclick as previously suggested works fine, but as also commented on, using jQuery is the goal. I have used this method for now, until I can work out why the selector isn't triggering the event.
0

See the working demo over here jsfiddle. You might forgot to add jquery js file reference in your page.

Comments

0

But This works fine...

<html>
  <head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $('#filterStarDiv #hotelFilterForm #Star0').click( function() {
          alert('Checkbox 0 clicked');
        });
      });
    </script>
  </head>
  <body>
    <div id="starsRemoved" style="display:none">No Stars Removed</div>
    <div id="filterStarDiv">
      <h6>Click on the star ratings below to add or remove hotels in that category</h6>
      <form id="hotelFilterForm" action="" method="post">
        <input type="checkbox" id="Star0"  name="Star0" value="0 Star" checked="checked" /> 0 Star<br />
        <input type="checkbox" id="Star1"  name="Star1" value="1 Star" checked="checked" /> 1 Star<br />
        <input type="checkbox" id="Star2"  name="Star2" value="2 Star" checked="checked" /> 2 Star<br />
        <input type="checkbox" id="Star3"  name="Star3" value="3 Star" checked="checked" /> 3 Star<br />
        <input type="checkbox" id="Star4"  name="Star4" value="4 Star" checked="checked" /> 4 Star<br />
        <input type="checkbox" id="Star5"  name="Star5" value="5 Star" checked="checked" /> 5 Star+<br />
      </form>
    </div>
  </body>
</html>

Find the mistake you did.

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.