Jquery - Need help with my Selector

edvinson
edvinson used Ask the Experts™
on
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!



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Michel PlungjanIT Expert
Top Expert 2009

Commented:
I suggest you get all elements with class ok-class and see if the contain error
$(".ok-class").text()

Commented:
Why not just validate the inputs based on value as part of your submit function instead of doing so much DOM manipulation?
Michel PlungjanIT Expert
Top Expert 2009

Commented:
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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
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/
Top Expert 2015
Commented:
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

Michel PlungjanIT Expert
Top Expert 2009

Commented:
mrh: how about

 $('#text-box').css('background-color', textBox==0?"red":"");

Author

Commented:
Between the two solutions, I was able to achieve my goal. Thank you both.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial