Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .append() does not work from .js file

I have the following line of code:

$('#text').append('<h2 style="color:white">afdghadfg</h2>');

If I paste this code inside of a script tag in the html, it works fine. However, when placed inside a .js file it does nothing.

Know that the .js file has plenty of other working javascript and jQuery code, it is only this line that won't work.

like image 404
Yottagray Avatar asked Nov 19 '25 00:11

Yottagray


2 Answers

Try this in your JS file:-

$(document).ready(function(){
    $('#text').append('<h2 style="color:white">afdghadfg</h2>');
});
like image 159
limc Avatar answered Nov 20 '25 16:11

limc


Could be the context you are putting it. Make sure it's not wrapped in a function and directly executable. Also check the JavaScript console (Firebug / CDT) to see if there are any errors.

Another thing to check is if it's getting executed before DOM ready, but I can't really tell you without seeing more code.

like image 43
Josh K Avatar answered Nov 20 '25 16:11

Josh K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!