Robert Granlund
asked on
JQuery add Class
I want to add a class to a text area if another field is filled in. I don't believe the following is the correct way of doing it.
function findSum() {
var myBox10 = document.getElementById('valueOfAccesories').value;
if(myBox10 != '') {
$("#attachedAccesories").addClass("required";)
}
}
Seeing the name of your class I think you want :
function findSum() {
if( $.trim( $('#valueOfAccesories').val() ).length > 0 ) { // we remove trailing spaces
$("#attachedAccesories").addClass("required");
}
}
ASKER
I think that what I need to do is add an attribute to the Textarea:
if( $.trim( $('#valueOfAccesories').val() ).length > 0 ) { // we remove trailing spaces
$("#attachedAccesories").attr("required");
}
title of the question is : << add a class >>
ASKER
@leakim well neither add class or attr work in this case.
how do you call the function?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to add the required attribute then
$("#attachedAccesories").a ttr("requi red",true) ;
$("#attachedAccesories").a
An attribute is not the same as a class. In this statement, ABC is a class, used with Cascading Style Sheets to add styling. And type is an attribute. It's coincidental that the type attribute also creates visual styling.
<input name="xyz" class="ABC" type="hidden" />
<input name="xyz" class="ABC" type="hidden" />
Open in new window