ksg
ksg

Reputation: 4067

get child('id') having a particular style in jquery?

how do i get the id child div whose style is display:block

<div id="mainDiv">
  <div id="step-3" style="display: none;">
  <div id="step-4" style="display: none;">
  <div id="step-5" style="display: none;">
  <div id="step-6" style="display: block;">
</div>

$(function(){
  var id= $("#parent").children("div")..... (im stuck).
})

Thanks!

Upvotes: 2

Views: 233

Answers (4)

Jai
Jai

Reputation: 74738

Well you can use :visible and .each to loop through every elem and get the id:

var elemID = $("#mainDiv").find("div:visible").each(function(){
                    return this.id;
             });

note:

you don't have an id as "#parent" so you have to use "#mainDiv".

Upvotes: 0

Mr. Alien
Mr. Alien

Reputation: 157334

You can use attr=val selector as well

$(function(){
    var id= $("#mainDiv div[style='display: block;']").attr("id");
    alert(id);
})

Demo

Or you can better use *= which will search for a sub string instead of matching the string like

$("#mainDiv div[style*='display: block;']").attr("id");

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

you can use :visible selector

var id= $("#parent").children("div:visible").attr('id')

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

You need to use :visible selector to achieve your objective.

Try,

var id = $("#parent").children("div:visible").attr('id')

Please read here for more reference.

Upvotes: 3

Related Questions