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
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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.