Find a string in div class with JQuery

Im not so hot with JQuery selectors... So i found this real useful, was looking for the id attribute of the ahref with a class that contained "selected".



var selectedTab = $('#YOURID').find('a').filter('[class~="selected"]').attr('id');


Bookmark and Share
  1. Azadi Saryev

    #1 by Azadi Saryev - November 23, 2011 at 2:00 AM

    Shorter version:

    $('#YOURID a[class~="selected"]').attr('id');

    Azadi
  2. Patrick

    #2 by Patrick - November 23, 2011 at 4:15 AM

    Hiyas

    Would it be better to use:

    var selectedTab = $('#YOURID a[class~="selected"]').attr('id');

    regards

    Patrick
  3. Tim Leach

    #3 by Tim Leach - November 28, 2011 at 5:17 PM

    The incredible thing about jQuery is you can simplify that snippet to this:
    $('#YOURID a[class~="selected"]').attr('id');

    You build the selector just like you would in CSS.
(will not be published)