-1

How can I attach a specific handler for each clicked anchor tag, and then turn off all anchor tags grouped under one div with a class name?

How can I attach a specific handler for each clicked anchor tag, and then turn off all anchor tags grouped under one div with a class name?

<div id="x">
    <a id="a">link one</a>
    <a id="b">link two</a>
    <a id="c">link three</a>
</div>

jquery:

$('#a').on('click touchstart', handlerfuncA );
$('#b').on('click touchstart', handlerfuncB );
$('#c').on('click touchstart', handerfuncC );

$('x').off('click touchstart' );

then reattach all without specifically attaching each anchor again. but how would i easily reattach each handler? i need the handlers to be reattached but was wondering if there was an easy way without specifying attaching every handler again.

Masu
  • 1,568
  • 4
  • 20
  • 41
  • What have you tried? The documentation for `off` should link to `on`, and it should be clear from then on. – John Dvorak Dec 24 '12 at 23:26
  • Show us the representative HTML and I'm sure we can either design a CSS3 selector to solve the issue or some code in the beginning of the click handler to skip the items you don't want the click handled for. – jfriend00 Dec 24 '12 at 23:27
  • say 3 anchors with id's a,b,c all grouped under a div x. $('#a').on('click touchstart', handlerAfunc ); $('#b').on('click touchstart', handlerBfunc ); $('#c').on('click touchstart, handlerCfunc ); $(#x).off( 'click touchstart' ); but how would i easily reattach each handler? i need the handlers to be reattached but was wondering if there was an easy way without specifying attaching every handler again. – Masu Dec 24 '12 at 23:27
  • It's still not clear what you want. Do you want a handler for all anchors except those that have a parent with a particular class? – tvanfosson Dec 24 '12 at 23:28
  • Updated your question to reflect what you actually want. – Ohgodwhy Dec 24 '12 at 23:33
  • How are you looking to reattach the delegated functions? You could easily make a helper to perform the above methods. We just need to see an example of how you're looking to reattach the event. Another click function? A condition? It's a bit vague at the moment to reliably answer. – Ohgodwhy Dec 24 '12 at 23:34
  • I'd suggest you back up and describe (in your question), the actual problem you're trying to solve. – jfriend00 Dec 24 '12 at 23:36
  • each anchor link has a specific handler, because its called with its specific attributes, but i wanted to just temporarily turn off all anchor tags, do some animation when one of the anchor tags is clicked, and then turn back on all the anchor tags after the animation is complete – Masu Dec 24 '12 at 23:44
  • under that one div which contains all the anchor tags, basically temporarily turn off all anchor tags under a div, do some animation, turn them back on, so one of them can be clickable again, at a time – Masu Dec 24 '12 at 23:46
  • I think your question is about rebinding? Some answers are here -> http://stackoverflow.com/questions/2593195/how-do-i-rebind-the-click-event-after-unbindclick – Ömer Faruk AK Dec 24 '12 at 23:49
  • yup that answers my question, thank you. i can just return if something is being animated. – Masu Dec 24 '12 at 23:53

2 Answers2

0

What you're doing doesn't make sense. Better to attach a single event handler to your group of anchors or your div, and then determine what to do based on the anchor's id, value, etc.

Unless, of course, the events of these anchors are entirely unrelated to each other... in which case it makes sense to unbind them individually (not as a group, as you want to do).

ktm5124
  • 11,861
  • 21
  • 74
  • 119
0

Adding some html may help but her is the basic idea:

Use jQuery on to attach the events:

http://api.jquery.com/on/

Use off to remove them: http://api.jquery.com/off/

Example:

Remove event from anchors

$("body").off("click", "a", oldClickFunction)

Edit

$('#x').off( 'click','a', touchstart );

Ulises
  • 13,229
  • 5
  • 34
  • 50