Black
Black

Reputation: 20382

jQuery wrap not working with append?

  1. I have a p element <p id="test">Test</p> and wrap it inside a span by using wrap and save the new element under $test.
  2. I append $test to p#output.

Result: p element is getting appended, but it is not wrapped inside a span anymore.

$test = $("p#test").wrap("<span style='color:red'></span>");
$("p#output").append($test);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="test">Test</p>
<p id="output">OUTPUT:</p>

Upvotes: 0

Views: 1196

Answers (1)

stackoverfloweth
stackoverfloweth

Reputation: 6917

jQuery documentation says .wrap() returns the original set of elements for chaining purposes.

use something like this instead $("p#output").append($('p#test').parent());

Upvotes: 3

Related Questions