3

I want to replace all the spans with a div. When I run this code only one element gets replaced. How to change this code so that the script affects all the spans?

I have tried to use clone(), but it didn't work.

$flexBox = $("<div></div>");
$(".overlayBox").replaceWith($flexBox);

<span class="overlayBox"></span>
<span class="overlayBox"></span>
<span class="overlayBox"></span>

1 Answer 1

5

You can use replaceAll() method:

$flexBox.replaceAll(".overlayBox");​

DEMO: http://jsfiddle.net/4VHVC/

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

9 Comments

Your solution doesn't work at all, even the first element doesn't get replaced.
@RegisteredUser It works if you write it like vice versa. Check the demo from the answer.
@RegisteredUser Do you check DEMO from the answer? Mine works perfectly. div blocks are displayed in red.
@RegisteredUser Tested and it worked fine in FF12, Chrome and IE 7.
Sure the demo works, but the code fails to work inside my app
|

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.