Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append div to end of document with jQuery?

I would like to write a function using jQuery to append a div to the end of a web page. I would like to be able to use the function on many different pages.

I've written the following code, but it doesn't work:

    $(document).append('<div id="helloDiv"></div>');     $('#helloDiv').html('hello'); // does nothing     $('#helloDiv').css('top','100') // throws an error 

How do I fix this?

like image 485
Vivian River Avatar asked Jul 11 '11 13:07

Vivian River


People also ask

What is insertAfter in jQuery?

The insertAfter() method is an inbuilt method in jQuery that is used to insert some HTML content after a specified element. The HTML content will be inserted after each occurrence of the specified element. Syntax: $(content).insertAfter(target)

What is the difference between append and appendTo in jQuery?

The append (content) method appends content to the inside of every matched element, whereas the appendTo (selector) method appends all of the matched elements to another, specified, set of elements.

Which jQuery function adds HTML content outside a selection?

prepend() - insert content at the start of selected element (inside its HTML tags) . after() - insert content after the selected element (outside its HTML tags) .

Which jQuery DOM method is used to insert content after the selected elements?

The jQuery append() method inserts content AT THE END of the selected HTML elements.


1 Answers

It is meaningless to append to the document. Append to the document's body node instead, since this is where all the visible content is:

$(document.body).append('<div id="helloDiv"></div>'); 
like image 142
lonesomeday Avatar answered Oct 12 '22 10:10

lonesomeday