Jump to content

Changing div/html class by variable


amg182

Recommended Posts

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

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)

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.