0

HTML Code :

<div id="txtAge">
    <table>
    <tr>
    <td><input type="checkbox" onclick="checking()" id="ch1" value="1"><td>
    <td><input type="checkbox" onclick="checking()" id="ch2" value="10"><td>
    <td><input type="checkbox" onclick="checking()" id="ch3" value="12"><td>
    <td><input type="checkbox" onclick="checking()" id="ch4" value="3"><td>
    </tr>
    <tr>
    <td><input type="checkbox" onclick="checking()" id="ch5" value="221"><td>
    <td><input type="checkbox" onclick="checking()" id="ch6" value="130"><td>
    <td><input type="checkbox" onclick="checking()" id="ch7" value="132"><td>
    <td><input type="checkbox" onclick="checking()" id="ch8" value="3333"><td>
    </tr>
    </table>

    </div>

Javascript :

function checking(){
$('input[type="checkbox"]').on('change', function() {
            $(this).closest('tr').find('input').not(this).prop('checked', true);
            alert(this.value);
        });
}

This is my code. when i click the checkbox it alert one times, when i click other checkbox it alert 2 times.Why the onclick function trigger more than 1 time when i continue clicking the checkbox?

Here the demo:

http://jsfiddle.net/L9z9t04p/11/

5
  • 3
    Remove onclick="checking()" from your HTML. Commented Dec 17, 2015 at 7:41
  • @RayonDabre As I said to gurvinder372, in this case the function wouldn't be called at all as jQuery binder is placed inside that function, ... So this is not a full answer! Commented Dec 17, 2015 at 7:54
  • @neoselcev, I did not claim is as an answer..Just a hint ;) Commented Dec 17, 2015 at 7:56
  • 1
    Try this: jsfiddle.net/L9z9t04p/17 Commented Dec 17, 2015 at 7:57
  • i dun know why your code does not work for me..actually the table is created by javascript after successful ajax call.. Commented Dec 17, 2015 at 8:13

5 Answers 5

3

Remove the onclick="checking" as an inline attribute from the checkbox.

Both are going to run independently and you can add as many event listeners to an element as you want, so one will not automatically remove the other.

Also remove this checking method too, you can directly bind jquery.on event

       $('input[type="checkbox"]').on('change', function() {
            $(this).closest('tr').find('input').not(this).prop('checked', true);
            alert(this.value);
        });

or if you don't want to remove checking method and will call it on some condition then you need to ensure that checking method is invoked

function checking(){
$('input[type="checkbox"]').off('change' ); //if the checking() is called multiple times
$('input[type="checkbox"]').on('change', function() {
            $(this).closest('tr').find('input').not(this).prop('checked', true);
            alert(this.value);
        });
}
checking();
Sign up to request clarification or add additional context in comments.

5 Comments

This is not the full answer. He has his jQuery binder inside function, which wouldn't be called in case he'll remove that attribute.
@max-cheng I have updated the answer to address the concern raised in comments.
@ gurvinder372 it will checked all the checkbox instead of one.jsfiddle.net/L9z9t04p/18
@maxcheng what should it do then instead?
@ gurvinder372 my intention is one row only one checkbox is checked.
1

Whether you bind the event through a listener:

 $('input[type="checkbox"]').on('change', function() {

Or you do inline binding to a function:

<td><input type="checkbox" onclick="checking()" id="ch2" value="10"><td>

Don't perform both. What's happening is that, everytime you click on the checkbox, you are registering the function one more time to the event. So every click will alert one more time that the last time

Comments

1

The problem arise from the fact that you are binding change event handler on every inline change event handler. this will keep on adding an event handler and you will keep on getting multiple alerts in this case.

Remove the inline change event handler from your check box. As you are already binding it using jQuery.

HTML

<input type="checkbox" id="ch5" value="221">

Script

$(document).ready(function(){
    $('input[type="checkbox"]').on('change', function() {
        $(this).closest('tr').find('input').not(this).prop('checked', true);
        alert(this.value);
    });
});

Comments

0

remove onclick="checking()" from html and bind change event for check box only once

Comments

0

For each time you trigger the function checking, you're adding an event to all checkboxes. It will accumulate for each time you click. I tried this in your fiddle and it works:

$(document).ready(function() {
  $('input[type="checkbox"]').on('change', function() {
    $(this).closest('tr').find('input').not(this).prop('checked', false);
    alert(this.value);
  });
});

Also, remove the onclick call on your checkboxes.

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.