Jquery: Store the <script> tag after .find ()

I am trying to add ajax to WordPress using jquery-ajaxy plugin and I am stuck in one:

My page is loaded with ajax call, filtered and added to dom. In short, it will be:

data = this.state.Response.data; //full html of page returned by ajax
$mainContent = $(data).find("#content"); //we filter out what we need 
$('#content_div').append($mainContent); //we display it

Easy, but there are built-in js scripts in $ mainContent that alternate jQuery. I need them somehow.

Everything works if I just do

.append(data);  

But the data contains the full html (doctype, head, meta), which I cannot add. So, is there a way to make these tags work after .find()?

+1
source share
2 answers

Whenever you create a jQuery object from a string, script tags are automatically deleted. You can see the discussion here: JavaScript RegEx for div tags

, , , javascript match(), , .

data = this.state.Response.data; //full html of page returned by ajax

//Assuming your data looks like: ...<!--Content Start-->CONTENT<!--Content End-->...
//we filter out what we need 
data = data.match(/<\!\-\-Content Start\-\->(.*?)<\!\-\-Content End\-\->/)[1];

$('#content_div').append(data); //we display it
+2
0

All Articles