Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  172] [ 4]  / answers: 1 / hits: 33077  / 11 Years ago, wed, february 5, 2014, 12:00:00

I'm trying to write a simple function in javascript to check if an element exists in the DOM and if it does remove, and if it doesn't, append it to the page, so far i've got this



        if document.contains(document.getElementById(submitbutton) {
document.getElementById(submitbutton).remove();
} else {
lastDiv.appendChild(submitButton);
}


(lastDiv is just the div I want to append the div 'submitButton' to)
Yet i'm getting the error Uncaught ReferenceError: myFunction is not defined



Any help?
I'm aware this is a very newbie question, sorry about that


More From » element

 Answers
2

There is a syntax error in the code, if statements require parens



if (document.contains(document.getElementById(submitbutton))) {
document.getElementById(submitbutton).remove();
} else {
lastDiv.appendChild(submitButton);
}

[#72697] Tuesday, February 4, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenbilliec

Total Points: 324
Total Questions: 106
Total Answers: 94

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;