Solved

JQuery add Class

Posted on 2014-09-03
9
190 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The viewer will learn how to dynamically set the form action using jQuery.

757 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

20 Experts available now in Live!

Get 1:1 Help Now