Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove HTML Element from variable containing HTML?

I have got a jQuery var that contains HTML that I'm appending to a page.

I need to remove a HTML element before I append it to the page but I can't seem to filter it.

for example the variable contains;

<div class="block1"></div>
<div class="block2"></div>

I have tried filtering like this before I append:

var mydata = $(mydata).filter('.block1');

and

var mydata = mydata.filter('.block1');

but none of these work. Any suggestions?

like image 843
Sam Avatar asked Jul 18 '26 14:07

Sam


1 Answers

Filter applied directly to string sometimes give string result, its safe to add element to DOM and then apply filters. Assign html to some temporary element in the DOM, Get the filtered items from this temporary element and assign it to desired element.

$('#TemporaryElemntToAddHtml').html(mydata);

    $('#ElemntToAddHtml .block1').each(function(){
         $('#ElemntToAddHtml').append($(this));
    });

$('#TemporaryElemntToAddHtml').remove();
like image 136
Adil Avatar answered Jul 21 '26 02:07

Adil