Appendchild does like label

The .label. is killing the code, but I want to append the child as a label not just plain text. Please help.


function show_error_body(label, msg){
		removeChildren(label);
		document.getElementById("submitmessage").label.appendChild(document.createTextNode(msg));
	}
	

What is this “label”? Is it a DOM element? If so the “.label” bit is not the way to do things. Is it a child of “submitmessage”? Post some sample HTML which will help demurkify this.

Sorry for the confusion. I think it is a child of submit message.

The output I currently have…


<div id="submitmessage" class="right">
	<input type="submit" name="submit" value="Submit" />
        This field is required
</div>

This occurs when I use…


function show_error_body(label, msg){
		removeChildren(label);
		document.getElementById("submitmessage").appendChild(document.createTextNode(msg));
	}

The desired output…


<div id="submitmessage" class="right">
	<input type="submit" name="submit" value="Submit" />
        <label class="error">This field is required</label>
</div>

You actually have to create the Label element first.

For this you need document.createElement. Then you can append the new text node to it, then append the label to the div. Try it and if it doesn’t work I’ll help you.

I’m still lost, I tried…


	function show_error_body(label, msg){
		removeChildren(label);
		var labelerror = document.createElement(label);
		document.getElementById("submitmessage").labelerror.appendChild(document.createTextNode(msg));
	}

This also fails…


	function show_error_body(label, msg){
		removeChildren(label);
		var labelerror = document.createElement(label).appendChild(document.createTextNode(msg));
		document.getElementById("submitmessage").appendChild(labelerror);
	}

Did you visit the link I gave you? This is what you need (I think - it’s still not clear what the parameter “label” being sent to the function is):

function show_error_body(label, msg){
        removeChildren(label);
        var labelerror = document.createElement('label');
        labelerror.appendChild(document.createTextNode(msg));
        document.getElementById("submitmessage").appendChild(labelerror);
    }

No sorry. I must have missed your link. The code you recommend works but the label parameter was to removechildren first before adding the label again. In other words, if I click on the field and click off of it several times it will continue to add labels. I only want one of them to be added. For some reason it isn’t removing the previous label it just keeps adding them onblur. Any fix for this? Am I using the wrong parameters?

Here is my removechildren function


function removeChildren(element){
	if (element.hasChildNodes())
	{
		while (element.childNodes.length >= 1)
		{
			element.removeChild(element.firstChild);       
		}
	}
}

[/QUOTE]

Dude - this onblur stuff is completely new to me. You really have to look at it from the perspective of someone who is not familiar with your application.

if I click on the field

What field? You need to provide the code that involves all of this stuff - including the onblur events and so on. But don’t post absolutely all your entire code and expect me to wade through it - that’s not what I’m asking.

Maybe you can do it yourself though - if you only want to create things once, you have to check if they’re there first. Have you tried that?

Yes, I actually solved this issue. Thank you.