amg182 Posted October 16, 2011 Share Posted October 16, 2011 Hi. Complete beginer with java.... Is it possible to change a html class depening on variable using javascript. For example if a user left a form input blank then javascript would change the css class to highlight it red. Here is what i am working with: function validateForm() { var x=document.forms["form"]["fieldname"].value; if (x==null || x=="") { alert("Fields in red need to be filled in"); var class ="class='error'"; return false; } } Thanks guys Quote Link to comment Share on other sites More sharing options...
teynon Posted October 16, 2011 Share Posted October 16, 2011 You can change the field with a classname change or style changes, up to you. document.forms["form"]["fieldname"].className = "newClassName"; Or document.forms["form"]["fieldname"].style.border = "2px"; I would personally do the classname change and would use ID's rather than form elements. (document.getElementById("id").className) Quote Link to comment Share on other sites More sharing options...
amg182 Posted October 17, 2011 Author Share Posted October 17, 2011 Hi teynon. That worked a treat! Thanks a million mate! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.