Slopeside Creative
Slopeside Creative

Reputation: 9207

Get ID of filtered element

I have multiple elements that share a class. At any given time, one of them may be visible. I can determine that one of them is visible, but how do I get it's ID?

n is the ID of an element that has been clicked, 1-4. I want to see if n matches the ID of the visible element so that no action is taken if the matching element is already visible. If you have already clicked one of them and made something visible, clicking again should do nothing. So, which element in c is the visible element - need it's ID.

function manageVisible(n) {
       var x = n, i, c;
       c = $('.glowSafe:visible');
       if(c.length !=0){
           c.fadeOut(300).hide();
           $('#glowSafe' + x).fadeIn(800);
       }
       $('#glowSafe' + x).fadeIn(800);
   }

Upvotes: 1

Views: 77

Answers (2)

gdoron
gdoron

Reputation: 150253

var theId = $('.glowSafe:visible').prop('id'); // jQuery >= 1.6
var theId = $('.glowSafe:visible').attr('id'); // jQuery <  1.6

Now you can use the above:

if (n !== theId)
    ...

Upvotes: 1

Adriano Carneiro
Adriano Carneiro

Reputation: 58595

To get the ID of the visible element of that class:

var id = $('.glowSafe:visible').attr("id");

or, since you already have that the element in c variable:

c = $('.glowSafe:visible');
var id = c.attr("id");

Upvotes: 0

Related Questions