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 Link to comment https://forums.phpfreaks.com/topic/249207-changing-divhtml-class-by-variable/ 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) Link to comment https://forums.phpfreaks.com/topic/249207-changing-divhtml-class-by-variable/#findComment-1279738 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! Link to comment https://forums.phpfreaks.com/topic/249207-changing-divhtml-class-by-variable/#findComment-1279983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.