Jquery - Need help with my Selector

I am working on some custom form validation. I need help with my selector.

Here is my ideal solution ( in English ):

1. Get all Elements in #myForm that have a class containing the string "error". Include checkboxes and radio's.

2. For each of those elements:

     a) Check to see if the element has a class of "ok-class".

          [YES] Remove that class

     b) Add class "Errror" to element

     c) Find the very next SPAN element. Remove any classes in this tag. Add Class "span-error"


Thanks! This is a little complex for me. I think I could do this one statement at a time, but to write a "chained" statement is a little out of my league.

Can't wait to see some solution ideas!



LVL 1
edvinsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
I suggest you get all elements with class ok-class and see if the contain error
$(".ok-class").text()
0
mrh14852Commented:
Why not just validate the inputs based on value as part of your submit function instead of doing so much DOM manipulation?
0
Michel PlungjanIT ExpertCommented:
Maybe you show the validation?

I agree you need to toggle the classes in the validation, for example

function validate(theForm) {
  document.getElementById("email_errorSpan".style.display=isValidEmail(theForm.email.value)?"none":"block"
.
.

}

or similar
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mrh14852Commented:
I made this fiddle just to show an example that doesn't have to do a bunch of DOM changes...just my$.02.

http://jsfiddle.net/shqG9/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom BeckCommented:
Here's a sample page that does exactly what you are asking it to do. I don't need to know why you're doing it this way.
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<style type="text/css">
.allSpans {color:blue}
.Error {border:1px solid red;background-color:#fed}
.span-error {color:orange}
</style>
</head>
<body>
<form id="myForm" action="#">
<input type="checkbox" />
<span class="allSpans">Checkbox 1</span>
<br />
<input type="checkbox" class="ef-error" />
<span class="allSpans">Checkbox 2</span>
<br />
<input type="checkbox" />
<span class="allSpans">Checkbox 3</span>
<br />
<input type="checkbox" class="ok-class" />
<span class="allSpans">Checkbox 4</span>
<br />
<input type="checkbox" class="ab-error" />
<span class="allSpans">Checkbox 5</span>
<br />
<input type="radio" name="group1" class="ab-error" />
<span class="allSpans">Radio 1</span>
<br />
<input type="radio" name="group1" class="ok-class" />
<span class="allSpans">Radio 2</span>
<br />
<input type="radio" name="group1" />
<span class="allSpans">Radio 3</span>
<br />
<input type="radio" name="group1" class="ab-error" />
<span class="allSpans">Radio 4</span>
<br />
<input type="radio" name="group1" class="ab-error" />
<span class="allSpans">Radio 5</span>
<br />
<input type="text" />
<span class="allSpans">Textbox 1</span>
<br />
<input type="text" class="ab-error" />
<span class="allSpans">Textbox 2</span>
<br />
<input type="text" class="ok-class" />
<span class="allSpans">Textbox 3</span>
<br />
<input type="text" class="ok-class" />
<span class="allSpans">Textbox 4</span>
<br />
<input type="text" />
<span class="allSpans">Textbox 5</span>
<input type="button" id="changeClasses" value="Change Classes" />
</form>
<script type="text/javascript">
$("#changeClasses").click( function() {
    $("input").each( function() {
        if($(this).hasClass("ok-class")){
            $(this).attr("class", "Error");
            $(this).attr("checked", "checked");
            $(this).next("span").attr("class", "span-error");
        }
    });
});
</script>
</body>
</html>

Open in new window

0
Michel PlungjanIT ExpertCommented:
mrh: how about

 $('#text-box').css('background-color', textBox==0?"red":"");
0
edvinsonAuthor Commented:
Between the two solutions, I was able to achieve my goal. Thank you both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.