2

I have the following html:

<ul id="all-terminals">

        <li data-terminal-id="101" class="">
            <label>
                <input type="radio" name="terminal" class="all" data-terminal-id="101">
                <a ...></a>

               ...
            </label>
        </li>

        <li data-terminal-id="100" class="active">
            <label>
                <input type="radio" name="terminal" class="all" data-terminal-id="100">
                <a ..></a>

               ...
            </label>
        </li>

        <li data-terminal-id="102" class="">
            <label>
                <input type="radio" name="terminal" class="all" data-terminal-id="102">
                <a...></a>

              ...
            </label>
        </li>
    </ul>

I wrote the following listener:

$(document).on('click', '#all-terminals li label', function(){alert(123)});

when I click on label, I see that alert executes twise.

Why?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

4 Answers4

3

Clicking a label automatically creates a click event on an input within it.

Assuming all your labels have inputs, you can simply change to:

$(document).on('click', '#all-terminals li input', function(event) {
  alert(123);
});

Fiddle

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
2

Not exactly sure why, but mouseup works.

$(document).on('mouseup', '#all-terminals li label', function(){alert(123)});
KidBilly
  • 3,408
  • 1
  • 26
  • 40
1

I think bubbling is occurring in your case . Read about on Bubbling and Capturing

Ashok Mandal
  • 278
  • 2
  • 13
0

I'm not completely sure, but I think that when you click on the label, you hit 2 of its child overlapping elements. Because they are part of the label but still separate elements, it fires two click events on label.

On the fiddle, it appears the it's the [a] element overlapping the ...

James McDowell
  • 2,668
  • 1
  • 14
  • 27