tags:

views:

375

answers:

5

So I have a form;

<label for="name">Job Date</label><br /><input id="name" class="required" title="Enter a name!" name="name" type="text" />

and some Jquery to cycle through each input with class "required" and see if it is empty/blank and if so give this input a red border color, and also the label assosciated with it a red color;

function checkreview()
    { 
     var errors = "";
     $(".required").each(function (i) 
     {
      if($(this).val() == "")
      {
       errors += $(this).attr('title')+'<br />';
       $(this).css('border-color','#FF0000');
       //need to select previous label and set color to red    
      }

     });


}

So I need a selector that will select the previous label and allow me to give it .css('color','FF0000').

Any ideas how to select the previous label in JQUERY?

+2  A: 

did you try this:

$("input").prev("label").css('color','FF0000')
Sergei
yep I've tried using;$(this).prev("label").css('color','FF0000')no joy, thank you though.
Alex Crooks
A: 
$(this).prev().prev().css('color','FF0000');

But I would use a css class instead of applying style with jQuery:

<style>
div.form label {display:block;}
label.error {color:FF0000}
input.error {border-color:FF0000}
</style>

<div class="form">
<label for="name">Job Date</label>
<input id="name" class="required" title="Enter a name!" name="name" type="text" />
</div>

The the validation would change to:

function checkreview()
{   
        var errors = "";
        $(".required").each(function (i) 
        {
                if($(this).val() == "")
                {
                        errors += $(this).attr('title')+'<br />';
                        $(this).addClass("error");
                        $(this).prev().addClass("error");    
                }

        });
}
jrummell
I tried prev().prev() but still no joy, the input box border definately changes though.
Alex Crooks
+2  A: 

Since there is a <br /> element before the input element you can't use prev().

What about

$("input").prevAll("label").css('color','FF0000');

or you can select the label element with the attribute for

$("input").prevAll("label[for=" + $("input").attr ( 'id' ) + "]").css('color','FF0000');

prevAll: Find all sibling elements in front of the current element.

rahul
+1  A: 

How about selecting the corresponding label using the id of the input field. So you'll find the label wherever it is.

Replace your comment with this:

$('label[for="'+ $(this).attr('id') +'"]').css('color','#FF0000');
andre-r
this is what I originally thought, however this doesn't work and I really really think it should so I'm going to look deeper to see its not something else blocking it.
Alex Crooks
It works for me. Maybe something else is wrong? Does `$('label[for="name"]')` work for you?
andre-r
I mean, what does `$('label[for="name"]')` return?
andre-r
OK problem solved, the issue was I was missing the # from the color hex code!!
Alex Crooks
thank you very much everyone for your help!
Alex Crooks
you're right, updated answer
andre-r
A: 

Try this:

$('label[for='+ $(this).attr('id') +']').css('border','#ff0000 1px solid');

or add the label border style to the CSS because you can't see the border color unless you have the border type set... the label[for=ID] does work as previously answered.

fudgey