4

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

4
  • 1
    You know that data is a string in this context, right? So you need to parse it Commented Jan 23, 2016 at 8:09
  • tried this alert($(data).html().find('div.repeat').html()) Commented Jan 23, 2016 at 8:09
  • Simpler is to append in body using $('body').append(data) and then find or simpler $('.repeat') Commented Jan 23, 2016 at 8:10
  • @AdamAzad: He/she is parsing it: $(data) Commented Jan 23, 2016 at 8:34

6 Answers 6

6

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>

Sign up to request clarification or add additional context in comments.

2 Comments

thought find helps to find the corresponding element. Then what's the use of find?
@AvinashRaj, find() searches in descendant of elements, where as filter() filters in the elements i.e. $(data)
1

replace find with filter

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

find looks for children, filter looks at the sibling level

Comments

0

Try this

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>';
 
var HTMLobject = $('<div/>').html(data);
console.log(HTMLobject.find('div.repeat').html());
alert(HTMLobject.find('div.repeat').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

You can simply try with this alert($(data).html());

Comments

0

you are just missing wrapper class here that's why you got an error as undefined

try the following code

	var data = '<div><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></div>'
alert($(data).find('div.repeat').html())

This give me perfect alert

Comments

0
ALternate solution:

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>'
$('body').append(data);
alert($('body').find('div.repeat').html());

Fiddle link

https://jsfiddle.net/fgf9nyn0/18/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.