Solved

JQuery add Class

Posted on 2014-09-03
9
191 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
  • 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
 
LVL 82

Expert Comment

by:leakim971
ID: 40302263
title of the question is : << add a class >>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 500 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 108

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now