0

I want to add a div before div 1, but the script will nestled in a few divs deep in the code. Would this work in theory?

<div id="1">
   <div id="2">
       <div id="3">
           <style>
           #4 {...}
           </style>
           <script src="//code.jquery.com/jquery-1.10.2.js"></script>
           <script>     
               $( "#4" ).before( $( "#1" ) );
           </script>
       </div>
   </div>
</div>

thanks

scott.schaffer
  • 525
  • 5
  • 20

1 Answers1

2

This should inject a div element before the div with id 1.

<div id="1">
   <div id="2">
       <div id="3">
           <style>
           </style>
           <script src="//code.jquery.com/jquery-1.10.2.js"></script>
           <script>
             var $div = $('<div></div>').attr('id', '4'); 

             $div.insertBefore('#1');
           </script>
       </div>
   </div>
</div>
John
  • 4,512
  • 8
  • 48
  • 98