Justin White
Justin White

Reputation: 714

Find element ID by its style

I need to find an a-tag's ID by its style.

My element have the class .sort.

The element's style is programmatically added earlier in one of two get functions, and I have to find it in a third get function.

Upvotes: 1

Views: 195

Answers (2)

gdoron
gdoron

Reputation: 150253

var result = $('a.sort[style="foo"]').attr('id');

Or if you don't use inline styles(which you should not...):

var result = $('a.sort').filter(function(){
                            return $(this).css('foo') == valueYouAreSearching;
                        }).attr('id');

Note that searching by a style sounds awkward!
Just add the element a new class, and search for it by it's new class name:

var result = $('a.sort.newClass').attr('id');

Upvotes: 2

Andreas Louv
Andreas Louv

Reputation: 47099

If the element is the only one with the class .sort

var theID = $(".sort")[0].id;

Or you can do something like this:

var theID = $(".sort").filter(function() {
    return $(this).css("marginLeft") == 0;
})[0].id;

Gives you the first element's ID with class .sort and marginLeft set to 0

To ensure that this will never break even if you have no match:

var theID = ($(".sort").filter(function() {
    return $(this).css("marginLeft") == 0;
})[0]||{}).id;

Then ID will contain a DOM Element's id or undefined

Upvotes: 1

Related Questions