0

I have a div with text that I'm trying to replace using Javascript. It works in JFiddle, but not in my Javascript and HTML files for some reason. I can't understand why.

Here's a link to the JSFidle: https://jsfiddle.net/ZEZEME/wgt0ayqe/1/

Here's my actual code:

test.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="test.js"></script>
</head>
<body>
    <div id="parkLinks"><span></span></div>
</body>
</html>

test.js

$('#parkLinks span').text("Hi I am replace");

As you can see, it's very simple, but it doesn't work. The page is empty instead of showing "Hi I am replace"

user10844328
  • 65
  • 3
  • 8

1 Answers1

0

Your test.js is running before the page is loaded, try this in test.js:

(function(){
  $('#parkLinks span').text("Hi I am replace");
})()

You can also use JQuery's document.ready: http://learn.jquery.com/using-jquery-core/document-ready/

// A $( document ).ready() block.
$( document ).ready(function() {
    console.log( "ready!" );
});
mikeb
  • 10,578
  • 7
  • 62
  • 120