Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get() jquery got undefined in my case

enter image description here

var data = $.parseHTML(data.responseText);    
console.log(data) // above img is the result of this
var elem = $(data).find('#all-tickets').get(0);
console.log(elem)

I got undefiend, I don't know why. I've tried not to parse html use string to search my id, which is all-tickets, it doesn't work too. Any thought?

I also tried $(data).find('#all-tickets').parent();, doesn't work :(

like image 397
Jennifer Avatar asked Nov 10 '22 00:11

Jennifer


1 Answers

That seems an array and you are trying to parse the xml content with $.parseHTML(). Instead you have to either try creating a loop or just append the contents in the div and .find() the desired element:

var wrapper = $('<div/>', {
    html:data.join('')
});

console.log(wrapper.find('#all-tickets')[0]);

check the sample demo:

var arr = ['text', '<a href="#">aaa</a>', '<div id="targetDiv">targetDiv</div>'];

var div = $('<div/>', { html: arr.join('')});

$(document.body).append(div.find('#targetDiv')[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
like image 140
Jai Avatar answered Nov 14 '22 23:11

Jai