6

I've created a very simple jsFiddle example of a click handler being assigned to two radio buttons with:

    $(document).ready(function () {
        $(".title").on("click", function (event) {
            alert('clicked');
        });
    });

As you can see, each time a radio button is selected the handler is called twice, why?

<label class="title">
    <input type="radio" name="heading" checked="checked" />Introduction and General Information about the Marketing Tool
</label>
<label class="title">
    <input type="radio" name="heading" />Implementation Steps of the Marketing Tool
</label>
James Montagne
  • 77,516
  • 14
  • 110
  • 130
Dónal
  • 185,044
  • 174
  • 569
  • 824

7 Answers7

6

You're using the title class in both labels, which means that it is used on both radio boxes. When you click it, it fires the event on both radio boxes. You should do your work with the radio button selector:

$(document).ready(function () {
    $(":radio").on("change", function (event) {
        alert('clicked');
    });
});

demo

reference change

by click

$(document).ready(function () {
    $(":radio").on("click", function (event) {
        alert('clicked');
    });
});

demo

Bono
  • 4,757
  • 6
  • 48
  • 77
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
3

This happens because your <input> tag is inside the <label> tag; since clicking the label triggers a click on the radio as well, you are basically clicking the radio two times.

If you move the <input> tag out of the <label> tag, you should be ok.

Raibaz
  • 9,280
  • 10
  • 44
  • 65
1

Use Change Event. It will work fine.

$(document).ready(function () {
    $(".title").on("change", function (event) {
        alert('clicked');
    });
});
Sarcastic
  • 677
  • 5
  • 14
1

Try

$(".title input").on("click", function (event) {
  alert('clicked');
});
beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
0

The problem is comming from <label> because when you click on label , radio is clicked as well and vise versa. If you change <label> to <p> ,this problem will be solved : DEMO HERE

<p class="title">
    <input type="radio" name="heading" checked="checked" />Introduction and General Information about the Marketing Tool
</p>

<p class="title">
    <input type="radio" name="heading" />Implementation Steps of the Marketing Tool
</p>

Or just try to add click event only to input like this :

$(document).ready(function () {
    $(".title input[type='radio']").on("click", function (e) {
        alert('clicked');
    });
});
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
0

It's to do with the scope of the title element I thing.

If you add a class to the inputs, and then bind the event to that it'll work correctly.

HTML:

<label class="title">
    <input type="radio" class='checkbox' name="heading" checked="checked" />Introduction and General Information about the Marketing Tool</label>
<label class="title">
    <input type="radio" class='checkbox' name="heading" />Implementation Steps of the Marketing Tool</label>

Script:

    $(document).ready(function (e) {
        $(".checkbox").on("click", function (event) {
            alert('clicked');
        });
    });

Fiddle

SpaceDog
  • 3,249
  • 1
  • 17
  • 25
-1

did some modification and it is working fine... though your code is correct here is your example

    $(document).ready(function () {
        $("input[type=radio]").on("click", function (event) {
            alert('clicked');
        });
    });
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49
  • 1
    This isn't really true. The click event was not bound for both the button and the label, just the label. The issue is that clicking the label also fires a click on the radio button. So it fires twice, once for the click on the label, then the click fired on the radio bubbles up to the label and fires again. The radio button itself does not have a handler bound. – James Montagne Sep 13 '13 at 13:47
  • thanks @JamesMontagne i had two fiddles opened so got confused. anyways i will me more carefull next time. :) – Deep Sharma Sep 13 '13 at 13:50