Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind to both blur and change, but only trigger a function once in Jquery/Javascript?

I'm trying to trigger a function when a select element is changed.

Since Ipad is having trouble with on('change'), I also want to bind to 'blur', which works fine on Ipad.

However I don't want both events to trigger the function twice, so I need some kind of hook to make sure if both change and blur trigger, that the underlying function is only fired once.

This is what I'm doing now, but ... not very nice:

// make sure binding is only assigned once
var compSel = $('#my_select');
if ( compSel.jqmData('bound') != true ){
    console.log("bound");
    compSel.jqmData('bound', true)
        .on( $.support.touch ? 'blur' : 'change', function(){
            console.log("trigger");
            // run function xyz
        })
    }               

This works if you can live with all touchable devices making do with blur.

Question:
Does anyone have a better idea to make sure blur and change only trigger a function once?

Thanks for help!

like image 292
frequent Avatar asked Jul 05 '12 14:07

frequent


People also ask

What is the purpose of the jQuery blur () event?

jQuery blur() Method The blur event occurs when an element loses focus. The blur() method triggers the blur event, or attaches a function to run when a blur event occurs. Tip: This method is often used together with the focus() method.

What is difference between Blur and change event?

onblur fires when a field loses focus, while onchange fires when that field's value changes. These events will not always occur in the same order, however. In Firefox, tabbing out of a changed field will fire onchange then onblur, and it will normally do the same in IE.

What is difference between on and bind in jQuery?

on() method is the preferred method for attaching event handlers to a document. For earlier versions, the . 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 .


2 Answers

don't know about ipad, but on browser maybe something like this

$("#dataTable tbody").on("click blur", "tr", function(event){
        if (event.type == "click")
        { 
        //do stuff
        }
        else
        {
        //do nothing
        }
});
like image 22
max4ever Avatar answered Sep 20 '22 16:09

max4ever


Try creating a function, bound to both events, and adding a timeout to call the function. This way, if the function is called multiple times, it will only run once.

Something like this (you can adjust the timeout if needed):

function blurChange(e){
    clearTimeout(blurChange.timeout);
    blurChange.timeout = setTimeout(function(){
        // Your event code goes here.
    }, 100);
}

$('#my_select').on('blur change',blurChange);
like image 54
Rocket Hazmat Avatar answered Sep 20 '22 16:09

Rocket Hazmat