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

jQuery check if item is currently selected

Sam Deering
Share

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
console.log($(this).is(":focus"));

//this is another way
function itemHasFocus(id)
{
    var output = false;
    console.log(id);
    //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