Solved

JQuery add Class

Posted on 2014-09-03
9
194 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
How our DevOps Teams Maximize Uptime

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

 
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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
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 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.

710 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