-2

example

<script type="text/javascript">
$(document).ready(function () {
    $("#exp1").replaceWith($("#exp2"));
});
</script>


<div class="exp1">example 1</div>
<div class="exp2">example 2</div>

when I use that code, I lost exp2 ( mean example 2 not showing ), only display example 1 which has been replace with example 2.. how do I not lose and just replace exp1 with exp2, and displays the result as below

<div class="exp2">example 2</div>
<div class="exp2">example 2</div>

please help me .. thanks

Sandhi
  • 89
  • 2
  • 11

1 Answers1

1

You should clone() element then use the replaceWith method. Also you need to use class selector

Use

$(document).ready(function () {
    $(".exp1").replaceWith($(".exp2").clone());
});

Demo on JsFiddle

Bibek Lekhak
  • 61
  • 1
  • 7
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 4
    You know perfectly well why the answer was first downvoted. Unfortunately the ninja-edits don't leave a mark in the answer history, but the answer kept bouncing between using ids and classes. – JJJ Jun 18 '14 at 08:52
  • @Juhana But i wonder why did guys downvote the first post, posted by the user balach..? That is perfectly alright.. Isn't..? – Rajaprabhu Aravindasamy Jun 18 '14 at 08:53
  • @RajaprabhuAravindasamy Can't know for sure, but `[0].outerHTML` is pretty sketchy and isn't exactly the same as `.clone()`. – JJJ Jun 18 '14 at 08:57