🤯 50% Off! 700+ courses, assessments, and books

jQuery check if item is currently selected

Sam Deering

jQuery code snippets to check if element is selected (could be used to check if input is focused as well).

//this is one way to do it

//this is another way
function itemHasFocus(id)
    var output = false;
    //loop for all fields in the form
    $.each($('#'+id+' :input'), function(i,v)
       console.log($(this).is(":focus")); //check item has focus
       if ($(this).is(":focus"))
         output = true;
         return false; //return false skips out of the loop
    return output;
//returns true or false