Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery add Class

Posted on 2014-09-03
9
Medium Priority
?
208 Views
Last Modified: 2014-09-13
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

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40302139
function findSum() {
var myBox10 = document.getElementById('valueOfAccesories').value;

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

}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40302156
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

0
 
LVL 7

Author Comment

by:rgranlund
ID: 40302256
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

0
Stressed Out?

Watch some penguins on the livecam!

 
LVL 82

Expert Comment

by:leakim971
ID: 40302263
title of the question is : << add a class >>
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40302273
@leakim well neither add class or attr work in this case.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40302277
how do you call the function?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40302290
Test page : http://jsfiddle.net/vg63hf5y/1/
If you fill the first textbox, you must fill the second one

<form action="http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28511082.html" onsubmit="return findSum()">
    <input type="text" id="valueOfAccesories" name="valueOfAccesories" />
    <br />
    <input type="text" id="attachedAccesories" name="attachedAccesories" />
    <br />
    <input type="submit" />
</form>

Open in new window


function findSum() {
    if( $.trim( $('#valueOfAccesories').val() ).length > 0 ) {
        $("#attachedAccesories").addClass("required");
        return false;
    }
    return true;
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40302307
If you want to add the required attribute then

$("#attachedAccesories").attr("required",true);
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40303725
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" />
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question