Scenario: Using jQuery multiple binds are done for a specific event. When event is triggered specified function should fire once PER trigger.
Challenge: The way jQuery trigger works is that when trigger is called the associated function is fired same number of times as number of binds that event has. On EACH trigger the function should be fired only once. How can this be achieved. "one" does not work in the scenario because the function should be fired each time the event is triggered.
Thanks in advance.
UPDATE: Apologize for not being more specific.
The requirement is that when page loads, different elements on the page to register so when a custom event occurs they will participate in it.
When the the custom event is triggered/raised, a function is to be called that will perform some task on the all the registered elements.
Eg. Please note, the elements embedded from different Partial Views (MVC).
<html>
<a href="#" id="triggerelement">Trigger element</a>
<div id="element1" data-join="aevent">something 1</div>
<div id="element2" data-join="aevent">something 2</div>
<div id="element3" data-join="aevent">something 3</div>
<div id="element4" data-join="aevent">something 4</div>
</html>
Two issues I am running into:
What element to "bind" to - $(?).bind. Reason: Multiple elements can raise this custom event and elements raising the event would/should not be known. One thought was - $(document).bind/$(document).trigger. Is this is the best approach?
Using jQuery when $().trigger is called how can I ensure that it invokes the associated function only once per call rather than for each element bound to that event.
have you tried to use namespaces when binding your functions?
$('a').bind("click.name1", function() {
alert("name1");
});
$('a').bind("click.name2", function() {
alert("name2");
$("a").unbind("click.name1");
});
http://jsfiddle.net/hssHZ/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With