JavaScript
Article
By Sam Deering

jQuery check if item is currently selected

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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

--ADVERTISEMENT--

//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
Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?