Reputation: 4067
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
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;
});
you don't have an id as "#parent"
so you have to use "#mainDiv"
.
Upvotes: 0
Reputation: 157334
You can use attr=val
selector as well
$(function(){
var id= $("#mainDiv div[style='display: block;']").attr("id");
alert(id);
})
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
Reputation: 388316
you can use :visible selector
var id= $("#parent").children("div:visible").attr('id')
Upvotes: 1
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