I've seen a lot about doing this in CSS (far from ideal), but I'm thinking of building a Chrome extension and it requires that a div is inserted prior to another (for clarification, I mean that when testing the extension I need to drag the div above the other using Chrome Dev Tools). Is there a way to do this upon page load using Javascript/JQuery (or some other way which can be done via Chrome Extension)?
In previous attempts I've tried using insertAfter()
/insertBefore()
and before()
and had no luck (although I might be missing something obvious), so I guess another question is do these need to be used in a certain way within a Chrome Extension?
Thanks in advance for any help!
I've seen a lot about doing this in CSS (far from ideal), but I'm thinking of building a Chrome extension and it requires that a div is inserted prior to another (for clarification, I mean that when testing the extension I need to drag the div above the other using Chrome Dev Tools). Is there a way to do this upon page load using Javascript/JQuery (or some other way which can be done via Chrome Extension)?
In previous attempts I've tried using insertAfter()
/insertBefore()
and before()
and had no luck (although I might be missing something obvious), so I guess another question is do these need to be used in a certain way within a Chrome Extension?
Thanks in advance for any help!
Share Improve this question edited Nov 25, 2021 at 0:58 Abdul Saleem 10.6k6 gold badges47 silver badges46 bronze badges asked Dec 17, 2012 at 1:16 whitfinwhitfin 4,6297 gold badges42 silver badges68 bronze badges2 Answers
Reset to default 11Here is a little demonstration.
The insertBefore
code being used is this:
$("<div>").prop("id","between").insertBefore("#blah");
The first two jQuery functions just create the div
element. If you want to move an existing one:
$("#existingDiv").insertBefore("#otherDiv");
And this should be pretty self-explanatory.
For plugins, load jQuery asynchronously. Something like this should do:
(function(){
var s = document.createElement('script');
s.src = 'http://code.jquery./jquery-1.8.2.min.js';
document.getElementsByTagName('head')[0].appendChild( s );
})();
jQuery has a very easy method to move one node before another, which you touched upon in your question:
BEFORE:
<div id="first"></div> <div id="second"></div>
$("#second").insertBefore("#first");
AFTER:
<div id="second"></div> <div id="first"></div>
As far as Chrome, you may want to do some digging to see if jQuery is loaded by the time you make your call or not