Insert DIV Just after tag

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Insert DIV Just after <body> tag



I have the following code:


<script type="text/javascript">
$(document).ready(function()
$('<div id="tools" style="text-align:right;float:right;"><input type="button" value="Print this page" onclick="window.print();return false;" /><input type="button" value="Save this page" onclick="go_saveas();return false;" /></div>').insertBefore('body');
);
</script>



Basically, I need to insert that whole Div just right after the <body> tag:


<body>


</head>
<body>
<div id="tools"..
...



Which works in Firefox but doesn't work in IE 7, what do I have to change to fix this?




3 Answers
3



You're using insertBefore. That will try to put it between head and body; not what you want. Try prependTo.


insertBefore


head


body


prependTo





+1 - but prependTo would probably make more sense here.
– Bryan Downing
Jun 3 '11 at 20:06





@Bryan: Whoops, my mistake. That's what I intended.
– icktoofay
Jun 3 '11 at 20:08



http://jsfiddle.net/XDFMt/:


<script type="text/javascript">
$(document).ready(function()

$('<div id="tools" style="text-align:right;float:right;"><input type="button" value="Print this page" onclick="window.print();return false;" /><input type="button" value="Save this page" onclick="go_saveas();return false;" /></div>')
.prependTo('body');

);
</script>



Instead of using insertBefore, using prependTo.
This way:


<script type="text/javascript">
$(document).ready(function()
$('<div id="tools" style="text-align:right;float:right;"><input type="button" value="Print this page" onclick="window.print();return false;" /><input type="button" value="Save this page" onclick="go_saveas();return false;" /></div>').prependTo('body');
);
</script>



The insertBefore inserts your code before the tag . That's why it gives you problems.
You were lucky that Firefox corrected it to what you wanted.
Now, prependTo inserts it inside your tag, but before all its content. ;)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard