Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector editing HTML before adding to the DOM

Tags:

jquery

I am trying to add some content to the DOM using a jQuery selector but I want to manipulate it before it gets added.

var $content = $('<h1>title</h1><h2 id="xx">remove me</h2><p>some content or other</p>');

$content.remove('#xx');

$('#output').append($content);

I have put this on jsfiddle here http://jsfiddle.net/NNcG5/1/

Many thanks, Mike

like image 847
Mike Mengell Avatar asked Dec 05 '25 18:12

Mike Mengell


2 Answers

I'd recommend $content.not('#xx').appendTo('#output');

I also updated your JS Fiddle with the solution too, here.

like image 173
Martin Avatar answered Dec 08 '25 07:12

Martin


Try like this:

var $content = $('<div/>').html($('<h1>title</h1><h2 id="xx">remove me</h2><p>some content or other</p>'));
$content.find('#xx').remove();
$('#output').append($content);

Live demo.

like image 43
Darin Dimitrov Avatar answered Dec 08 '25 08:12

Darin Dimitrov