ollierexx
ollierexx

Reputation: 509

Why wont this javascript run?

Someone created this code for me: http://jsfiddle.net/kzFWa/

var downloadButton = document.getElementById("continue");
var counter = 60;
var newElement = document.createElement("p");
newElement.innerHTML = "<h3>or click here to continue in 60 seconds</h3>";
var id;

downloadButton.parentNode.replaceChild(newElement, downloadButton);

id = setInterval(function() {
    counter--;
    if(counter < 0) {
        newElement.parentNode.replaceChild(downloadButton, newElement);
        //newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"
        clearInterval(id);
    } else {
        newElement.innerHTML = "<h3>or click here to continue in  " + counter.toString() + " seconds.</h3>";
    }
}, 1000);

However, if i change

newElement.parentNode.replaceChild(downloadButton, newElement); 

to

newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"

the code will no longer run. What am i doing wrong?

Upvotes: 0

Views: 92

Answers (2)

Irvin Dominin
Irvin Dominin

Reputation: 30993

You have a syntax error in:

newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"

Check the console and you'll see:

SyntaxError: missing ; before statement newElement.innerHTML = "Click Me To Continue"

so the code is not well formed, change it in:

newElement.innerHTML = "<a href='survey.php'>Click Me To Continue</a>";

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals

Demo: http://jsfiddle.net/aY5PK/

Upvotes: 0

MrCode
MrCode

Reputation: 64526

You have a syntax error in:

newElement.innerHTML = "<a href="survey.php">Click Me To Continue</a>"

Double quotes can't be used within a double quoted string like that. Either escape or change to a combination of single quotes and double quotes:

newElement.innerHTML = '<a href="survey.php">Click Me To Continue</a>';

Upvotes: 3

Related Questions