Link to home
Create AccountLog in
Avatar of kadin
kadinFlag for United States of America

asked on

How do I say, if node exists then remove it.

I want to replace one error message with another. The err variable always has only one error message in it. But as I submit the form multiple times to validate fields that were later deleted, the error messages are stacking up.

So I want to delete the text node (txt variable) and then recreate a new one and append it to errNode; However I think I need an if statement that checks whether the text node (txt variable) even exist first. How do I do that? Thanks.

I tried this and some other combinations, but it did not work for me.
if (errNode.txt.length > 0) {
   errNode.removeChild(txt);
}

// right here put:
// if var txt already exists and is appended to errNode,
// then remove and delete before recreating below.

var err = 'this is a big one';
var errNode = document.getElementById('padding');
var txt = document.createTextNode(err);
errNode.appendChild(txt);
			
//alert(errNode.txt.length);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sivagnanam chandrakanth
sivagnanam chandrakanth
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of kadin

ASKER

Thanks.