Zaheer Ahmad
Zaheer Ahmad

Reputation: 176

How to get inner <div> id from string

I know its a common issue but still having issue so need good suggestion or fix of it.

My Issue is

text = "<div class='catDiv'><div id='1689' class='cat'>Baby</div></div>";

Now I need inner div id from above string via jQuery.

catid = $(text).filter("div");
ct = $(catid).filter("div").attr("id");

I use this but can't get the id

Upvotes: 0

Views: 44

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can just find the div from the text variable so that it will return you the inner div then get the id:

var text = "<div class='catDiv'><div id='1689' class='cat'>Baby</div></div>";
var ct =  $(text).find('div').attr('id')
console.log(ct);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

Related Questions