jQuery HTML Element How to - Swap li elements with replaceChild possible








Question

We would like to know how to swap li elements with replaceChild possible.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--  ww w. ja v a2 s  .  c o m-->
    $('#item2').insertBefore('#item1');
});
</script>
</head>
<body>
  <ul id="mylist">
    <li id="item1">Item 1</li>
    <li id="item2">Item 2</li>
    <li id="item3">Item 3</li>
  </ul>
</body>
</html>

The code above is rendered as follows: