0

I have 4 divs in a container. I want to display the html of the container which contains the divs in the textarea. I'm able to do this. The issue is, i don't want to get all the html of the container. I don't want to get #iv #three. I want to copy all the html of the container except div #three. I could use $('#three').remove() but i don't want to remove the div, I just don't want to copy it's html value to textarea. Check jsfiddle http://jsfiddle.net/rzfPP/

<div id="container">
  <div id="one">test 1 </div>
  <div id="two">test 2 </div>
  <div id="three">test 3 </div>
  <div id="four">test 4 </div>
</div>    
<textarea id="save"></textarea>

var x = $('#container').html();
$('#save').val(x);

4 Answers 4

3

Try this

$("#container").clone().find("#three").remove().end().html();

http://jsfiddle.net/rzfPP/21/

Sign up to request clarification or add additional context in comments.

Comments

1
/*
var x = $('#container').html();
$('#save').val(x);
*/

var lol = $('#container').clone()
$(lol).find('#three').remove();


$('#save').val(lol.html());

Comments

1
$('#container').clone().find('#three').remove().end().html();

Technically this is illegal since you are duplicating IDs, but it works fine.

http://jsfiddle.net/rzfPP/33/

Edit: Someone beat me to it :( Oh well.

Comments

0
var text = "";

$('#container div').each( function() {

   if ( this.id != "three" ) {

      text += $(this).html();

   }

});

$('#save').val( text );

http://jsfiddle.net/rzfPP/31/

Basically you check the divs inside #container one by one, and check their id. If it's one you want, add their html to a string. Then at the end give that string as your textarea value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.