mike_g Posted May 14, 2007 Share Posted May 14, 2007 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 More sharing options...
jesh Posted May 14, 2007 Share Posted May 14, 2007 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 More sharing options...
mike_g Posted May 14, 2007 Author Share Posted May 14, 2007 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 More sharing options...
jesh Posted May 14, 2007 Share Posted May 14, 2007 Anyway, thanks.Glad I could help. Check out the HTML DOM tutorial for more information about changing styles through java script:DOM Style: http://www.w3schools.com/htmldom/dom_obj_style.aspGeneral DOM Reference: http://www.w3schools.com/htmldom/dom_reference.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.