Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
198
rated 0 times [  199] [ 1]  / answers: 1 / hits: 54750  / 13 Years ago, mon, july 11, 2011, 12:00:00

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?


More From » jquery

 Answers
13

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>');

[#91248] Saturday, July 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;