Solved

JQuery add Class

Posted on 2014-09-03
9
193 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 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 109

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress French and English Site 6 79
jQuery dialog box help 2 17
Understanding UserAgent String 2 18
Can't get page to reload 9 13
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

820 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