Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind function to DOM events once and only once so that they will not execute for a second time in the triggering of event?

so I have anchor tags in the form of <a href='[link'] rel='Tab'> and I apply the following at page load:('document ready' I mean)

jQuery("a[rel*=Tab]").click(function(e) {
                e.preventDefault();//then I do some stuff to open it in jq ui tab}

now the problem is that when I do this and later through javascript new links are generated, in my case with loading a second page with JqGrid there are new <a rel='neoTab'>'s that did not exist when I first run jQuery("a[rel*=Tab]").click(function(e) so they won't work... so I can run jQuery("a[rel*=Tab]").click(function(e) at every event that creates new links but then the old links would load multiple tabs so is there a way I could select all the "a[rel*=Tab]"s that were not not selected before?

note: I can and have already solved this through an algorithmic approach as you can see through the details below, I just think there is some syntaxes I am not aware of to prevent the need to use this hack!

Unnecessary Details:

var neoTabitStat = 0;
var openTabs = new Array();
openTabs[0] = 'inbox';
if(!(currentBox=='inbox'||currentBox=='outbox') ) {var currentBox = 'inbox';}

function initNeoTab(a){
    if(neoTabitStat==0) {
        jQuery("a[rel*="+a+"]").click(function(e) {
            e.preventDefault();
            tabIt(jQuery(this).attr('href')+'&nohead=1',jQuery(this).attr('title'),jQuery(this).attr('data-hovercard'));
        });
    neoTabitStat++;
    }
}

function tabIt(a,b,c) {
    c = typeof(c) != 'undefined' ? c : 0;
    lastOpen = openTabs.length;
    if(lastOpen<6) { 
        if(openTabs.indexOf(c)<0) {
            openTabs[lastOpen] = c;
            jQuery("#tabs").tabs("add" , a , b, lastOpen+1 );
        }
        $tabs.tabs('select', openTabs.indexOf(c) );
    }else{
        var tabErrDig = jQuery('<div style="display:hidden">You have opened the maximum amount of tabs please close one to open another tab.</div>').appendTo('body');
        tabErrDig.dialog({width:500,title:'Maximum Tabs Opened',modal: true,
                buttons: {
                    Ok: function() {
                        jQuery( this ).dialog( "close" );
                    }
                }
        });
    }
}

jQuery(document).ready(function() {  initNeotab('nameOfrelAttr4existingLinks'); }
myGrid = jQuery("#list").jqGrid({/*alot of stuff...*/} , gridComplete: function() { initNeotab('nameOfrelAttr4generatedLinks');
}

UPDATE: as Wrikken suggested jQuery().live I checked it out and it is exactly what I need but I can't get it to actually work, it won't work in some cases. For everyone else who wrote different solutions, are you fimiliar with .live() if so why is it that I can't use it here?

CONCLUSION special thanks to fudgey, patrick dw, and wrikken. All answers were helpful. .delegate(). worked for me but for some reason .live() didn't. something about the way jqGrid add's elements to the table. I'm gonna keep the question open for a while. Thanks alot again for all your help. +1 to all

like image 405
Neo Avatar asked Oct 16 '10 23:10

Neo


People also ask

Which jQuery keyword should be used to bind an event to an element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.

Which jQuery event listener can you use to bind the value in an input field to a variable?

Use jQuery . submit() to modify the value at the time of form submission (value is modified before submit, then submit continues).


1 Answers

In jQuery, use the live() function

like image 84
Wrikken Avatar answered Sep 25 '22 17:09

Wrikken