Hunter
December 9th, 2009, 06:03 AM
I have a form, as shown below. The fields are named "name", "email" ect..
<form name="form1" method="post">
<p class="g2">Name:</p>
<input type="text" class="contact-feild" name="name" size="20" id="name" onBlur="validate(name)"/>
<br />
<p class="g2">Email:</p>
<input type="text" class="contact-feild" name="email" size="20" />
<br />
<p class="g2">Message:</p>
<textarea name="message" class="contact-feild" style="width:300px; height:150px; overflow:scroll; overflow-x:hidden;">
</textarea>
<br />
<input type="submit" value="Submit" class="button" name="submit"/>
</form>
I was wondering if I could make a div named "namediv", "emaildiv" ect.. as the id.
This saves the script confusing the div with the form field. But I don't want to have loads of javascript to show the divs seperatly.
This is hard to explain, but is there a way I can use the document.getElementById() statment, and make it pull in the id, from the field which the javascript is already using. But add it infront of some text, eg:
document.getElementById(id+div).
Or
document.getElementById(id||div).
So it would basically look like this when run through the script:
document.getElementById(namediv)
And
document.getElementById(emaildiv)
Here is my javascript:
// VALIDATION ------------------------------------------------------------------------------------------
function validate(id) {
if (document.form1.name.value == "")
{
document.getElementById(id).style.backgroundColor= '#3f2222';
}
else {
document.getElementById(id).style.backgroundColor= '#2c482b';
return(true);
}
}
// VALIDATION END -------------------------------------------------------------------------------------
<form name="form1" method="post">
<p class="g2">Name:</p>
<input type="text" class="contact-feild" name="name" size="20" id="name" onBlur="validate(name)"/>
<br />
<p class="g2">Email:</p>
<input type="text" class="contact-feild" name="email" size="20" />
<br />
<p class="g2">Message:</p>
<textarea name="message" class="contact-feild" style="width:300px; height:150px; overflow:scroll; overflow-x:hidden;">
</textarea>
<br />
<input type="submit" value="Submit" class="button" name="submit"/>
</form>
I was wondering if I could make a div named "namediv", "emaildiv" ect.. as the id.
This saves the script confusing the div with the form field. But I don't want to have loads of javascript to show the divs seperatly.
This is hard to explain, but is there a way I can use the document.getElementById() statment, and make it pull in the id, from the field which the javascript is already using. But add it infront of some text, eg:
document.getElementById(id+div).
Or
document.getElementById(id||div).
So it would basically look like this when run through the script:
document.getElementById(namediv)
And
document.getElementById(emaildiv)
Here is my javascript:
// VALIDATION ------------------------------------------------------------------------------------------
function validate(id) {
if (document.form1.name.value == "")
{
document.getElementById(id).style.backgroundColor= '#3f2222';
}
else {
document.getElementById(id).style.backgroundColor= '#2c482b';
return(true);
}
}
// VALIDATION END -------------------------------------------------------------------------------------