Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Move Element With Script Element Without Re-Executing Script with jQuery?

I have this code snippet:

  <div id="div1">
  </div>

  <div id="div2">
    <h3>This is the content</h3>
    <script type="text/javascript">
      alert('This is the content');
    </script>
  </div>

  <script type="text/javascript">
    jQuery('div#div2').appendTo('div#div1');
  </script>

Using this code, alert message will be displayed twice (once when the page is loading, and then when jQuery re-execute the script when it execute appendTo method?

Any idea of how to use jQuery to conveniently move element (that have script tag) around without re-executin the javascript?

Thanks.

like image 370
Nordin Avatar asked Jun 25 '09 03:06

Nordin


1 Answers

Once the <script> has executed you don't need it any more, so you could remove it before moving #div2.

$('#div2 script').remove();
$('#div2').appendTo('#div1');
like image 100
John Kugelman Avatar answered Oct 30 '22 04:10

John Kugelman