Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

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";)	
}

}

Open in new window

Avatar of Gary
Gary
Flag of Ireland image

function findSum() {
var myBox10 = document.getElementById('valueOfAccesories').value;

if(myBox10 != '') {
	$("#attachedAccesories").addClass("required")	// you had a ; here
}

}

Open in new window

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");
    }
}

Open in new window

Avatar of Robert Granlund

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");
    }

Open in new window

title of the question is : << add a class >>
@leakim well neither add class or attr work in this case.
how do you call the function?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to add the required attribute then

$("#attachedAccesories").attr("required",true);
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" />