Jump to content

Classes not updating


mike_g

Recommended Posts

I'm making a form. If the user submits it and fields are invalid it cancels the action. I then want to highlight the invalid fields. Heres my function to update the text:

function InvalidChange(errors){ 		var e = errors.split(' ');		for(var i=0; i<e.length-1; i++)		{				if(e[i]=="Surname")e[i]="FirstName";				document.getElementById(e[i]+"_X").innerHTML = "<tr class='errorx'>";		}}

All the elements are being found. If I add some text it will add the text, it just wont change (or display the change of) the class.Heres an example of a span to update in the HTML:

<span id="House_X"><tr></span>

Anyone know how I can either get this to work, or do something else to the same effect? Cheers.

Link to comment
Share on other sites

You're going to want to avoid markup like this:

<span id="House_X"><tr></span>

First, it's not valid XHTML, second you shouldn't put block level elements inside inline elements.If you are trying to change the css class for the element so that the user can visually see that there is an error, you might try this:

document.getElementById(e[i]+"_X").className = "errorx";

Or, to add a little bit more error handling:

var span = document.getElementById(e[i]+"_X");if(span){	span.className = "errorx";}

I hope this helps.

Link to comment
Share on other sites

Yep, that works nicely.I just started using XHTML and I guess it was probably stopping me from editing the markup like that, as its an easy way to screw up a page if theres errors in it. I Originally tried updating the elements class, but I did it wrong:

document.getElementById(e[i]+"_X").class

Anyway, thanks.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...