afdghadfg'); ...">

JQuery.append () not working from .js file

I have the following line of code:

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

If I embed this code inside a script tag in html, it works fine. However, if it is placed in a .js file, it does nothing.

Be aware that there are many other working javascript and jQuery codes in the .js file, only this line will not work.

+3
source share
2 answers

There may be a context that you put it. Make sure that it is not wrapped in a function and directly executable file. Also check your JavaScript console (Firebug / CDT) to see if there are any errors.

Another thing to check is to execute it until the DOM is ready, but I cannot tell you without seeing more code.

+3
source

Try this in your JS file: -

$(document).ready(function(){
    $('#text').append('<h2 style="color:white">afdghadfg</h2>');
});
+5
source

Source: https://habr.com/ru/post/1790749/


All Articles