Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a specific element using jquery?

I'm trying to get the div element which has the class repeat. So I tried this, but it shows undefined.

var data = '<div class="test form-group col-xs-6 repeat" id="repeat_py">\n <div class="kv col-lg-2">index:\n<pre class="num">0</pre></div>\n<div><span class="kv">value: <pre class="num">m</pre></span></div></div>'

alert($(data).find('div.repeat').html())

Fiddle

like image 650
Avinash Raj Avatar asked Mar 14 '23 12:03

Avinash Raj


1 Answers

You need .filter() instead of .find()

Reduce the set of matched elements to those that match the selector or pass the function's test.

 $(data).filter('div.repeat').html()

var data = '<div class="test form-group col-xs-6 repeat" id="repeat_py">\n <div class="kv col-lg-2">index:\n<pre class="num">0</pre></div>\n<div><span class="kv">value: <pre class="num">m</pre></span></div></div>'

alert($(data).filter('div.repeat').html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
like image 79
Satpal Avatar answered Mar 23 '23 09:03

Satpal