If you don't need to remove the click functions then they can simply
remain in place, and therefore do not need unbinding/re-binding; but
you should return false from the click handler to prevent the click
event going any further than the intended handler ...

$("#elementA").click( function () {
    $("#elementB").show();
    return false;
});
$("#elementB").click( function () {
    $(this).hide();
    return false;
});

or even...

$("#elementA, #elementB").click( function () {
    $("#elementB").toggle();
    return false;
});

If you do want to remove the click handler, and re-bind after each
click, then...

function showB(){
  $('#elementB').one('click', function () {
      $('#elementA').one('click', showB);
      $(this).hide();
      return false;
    }).show();
  return false;
}
$("#elementA").one('click', showB);


On Nov 15, 9:45 am, Shawn <[EMAIL PROTECTED]> wrote:
> Try this:
>
> $("#elementA").unbind("click").click( function () {
>    $("#elementB").show();
>
> });
>
> $("#elementB").unbind("click").click( function () {
>    $(this).hide();
>
> });
>
> The problem is not event "bubbling" per se.  It's more a case of you are
> adding element B, then applying event handlers.  At a later time, you
> again add element B and apply event handlers again.  You now have two
> event handers assigned.
>
> Using the sample above, you are explicitly saying to unbind the "click"
> event handler - so that there is NO handler for the click event.  Then
> we apply a click handler afterwards.  This approach ensures only one
> handler is fired.
>
> Of course, this may not be quite what you want in all cases, but I trust
>   you to know your needs.. :)  Also, a jQuery guru may correct me and
> offer an easier method....
>
> HTH
>
> Shawn
>
> julio wrote:
> > Hi,
>
> > I have this situation:
>
> > When I click on an element (A), a new element (B) of same type is
> > overlayed to A on foreground.
> > When I click on B, B disappares and remain A on foreground. When I re-
> > click on A 2 elements B are overlayed instead of 1.
>
> > I guess this is a problem of event-bubbling, and so instead of use
> > "bind" to catch click event of mouse, I use "one" for A and B.
> > Obviously "one" permits only one click for an element, and so I cannot
> > click a second time on A or B. How can I reset "one-condition"
> > everytime on element is on foreground? Or this is not event-bubbling
> > problem and solution is another?
>
> > Thanks,
> > Julio

Reply via email to