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 tried not to parse the html usage string to find my identifier, which is an all-ticket, it does not work either. Any thought?

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

+4
source share
1 answer

It seems to be an array, and you are trying to parse the contents xmlwith $.parseHTML(). Instead, you need to either try to create a loop, or simply add content to the div and the .find()desired element:

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

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

check 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>
Run codeHide result
+1
source

Source: https://habr.com/ru/post/1619338/


All Articles