9

When i added one select into label and binding for click event, jquery fires twice.

http://jsfiddle.net/d8Ax7/

html:

<label class="lol">
    <div>
        bla
    </div>
    <div style="display:none;">
        <select></select>
    </div>
</label>

javascript:

$("label.lol").on({
    click : function ()
    {
        alert(1);
    }
})

How can i fix this bug without adding "for" attribute to labels?

koopajah
  • 23,792
  • 9
  • 78
  • 104

9 Answers9

4

I know there are already many answers to this question but are vague.

$("label.lol").on({
    click : function ()
    {
        alert(1);
        return false;
    }
});

Ok, Everyone appreciated. Problem Solved. But Question arises Why return false;.

Answer In Simple Words: It is a way of telling computer that return from the function, the task i gave to you is completed. No more Mess now.

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • click event propagates or bubble up the DOM which makes it to run twice. One on its own and other select tag (or any other form elements) in between it. You can see this fiddle: http://jsfiddle.net/5QAS2/ **What is in it?** See that when `input` or any other tag is in between label element, the function runs twice. And does not when form elements are not in it. I haven't done a research on this. So, can't explain more or can't say that it is a bug or something. Yeah! – Muhammad Talha Akbar Feb 09 '13 at 08:02
  • And another thing is that if it should alert thrice if there are two inputs. Which it does not.See: http://jsfiddle.net/5QAS2/1/ So, i think its a bug. We can't say it is bubbling the DOM. Well, I suggest you to `return false;` when ever you write program. – Muhammad Talha Akbar Feb 09 '13 at 08:09
2

You could try using the label's "for" attribute to link it to a select outside of it. See my example

<label for="test" class="lol">
    <div>
        bla
    </div>
</label>

<div style="display:none;">
    <select id="test"></select>
</div>

http://jsfiddle.net/d8Ax7/1/

Roger C
  • 338
  • 1
  • 5
0
$("label.lol").off("click").on(//...

but if it's firing twice it surely means you have bound it twice so using .off is more a hack than the best solution to your problem which would be to understand how you end up binding twice this event... & surely the code you show doesn't explain this double bondage

edit : & in your fiddle it does indeed trigger only once...

mikakun
  • 2,203
  • 2
  • 16
  • 24
0

This way it works too but I'm don't fully understand what you're trying to do... And a label should only contain a label text for the select and not contain the select himself..

   $("label.lol div:first").on({
        click : function ()
        {
            alert(1);
        }
    })
soyuka
  • 8,839
  • 3
  • 39
  • 54
0

There are two options when overriding the click check out this stack overflow question for greater detail, but to sum it up try this...

$("label.lol").click(function () {
        alert(1);
        return false;
});
Community
  • 1
  • 1
cameronjchurch
  • 410
  • 3
  • 7
0

Your markup is horrible.. Your problem is that jquery is doing the click event on each div inside your label function - means twice. You should clean ur markup and ask the div you want to be clickable directly. Else use :first to make sure the event is only fired once:

$('.lol div:first').click(function ()
{
    alert("1");
})
gulty
  • 1,068
  • 1
  • 8
  • 14
  • You are right about the markup. But the event fires on the label. Try replacing the `alert("1")` with `alert(this)` and you will see that the it shows `[object HTMLLabelElement]` twice. – fragmentedreality Feb 08 '13 at 15:45
0

You should add e.preventDefault();

$("label.lol").on({
  click : function (e)
  {
    alert(1);
    e.preventDefault();
  }
});
Kaeros
  • 1,138
  • 7
  • 7
0

your problem is with your distinction between elements.e.g you should select the first child of label with :first-child selector, or give it an id:

HTML:

<label class="lol">
    <div id="first-child">
        bla
    </div>
    <div style="display:none;">
        <select></select>
    </div>
</label>

JS:

$("#first-child").on('click',function (){
        alert(1);
});

http://jsfiddle.net/d8Ax7/5/

Ramin Omrani
  • 3,673
  • 8
  • 34
  • 60
0

Try to filter with CSS: Add an intermediate class clicked that prevents the second alert (or whatever action) and than remove the .clicked to be ready for the next round.

$("label.lol").on({
    click : function ()
    {
        if (! $(this).hasClass('clicked'))
            alert(1);
        $(this).toggleClass('clicked');
    }
})
fragmentedreality
  • 1,287
  • 9
  • 31