views:

48

answers:

2

I have a form with labels and input fields. When clicking into a field I want to style the field with CSS and display some information about the input requirements in a div under the label.

The focus() and blur() events add the classes for styling the field just fine but trying to show/ hide the info divs triggers the methods on ALL fields using $(this).siblings()

$(".input-field").focus(function() { 
    $(this).addClass("input-field-focus");
    $(this).siblings(".label-info").show();
    return false; 
});
$(".input-field").blur(function() { 
    $(this).removeClass("input-field-focus");
    $(this).siblings(".label-info").hide();
    return false; 
});


<label for="login">
    User name:<br />
    <div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</div>
    <input type="text" name="login" class="input-field" value="<?php echo (isset($_POST['login'])) ? $_POST['login'] : ""; ?>">
</label>    
+2  A: 

EDIT:

Based on your updated code, you should be using .prev() instead of .siblings().

$(".input-field").focus(function() {
    $(this).addClass("input-field-focus");
    $(this).prev(".label-info").show();
    return false;
});
$(".input-field").blur(function() {
    $(this).removeClass("input-field-focus");
    $(this).prev(".label-info").hide();
    return false;
});

Original answer:

Your code works fine: http://jsfiddle.net/D9qnk/

My guess is that your are initially hiding the .label-info using visibility: hidden; instead of display:none;

If that's the case, switch your css to use display:none instead.

.label-info {
    display: none;
}​

Or if you want to use the visibility property, then change its value using .css():

$(this).siblings(".label-info").css('visibility','visible');

...

$(this).siblings(".label-info").css('visibility','hidden');
patrick dw
Voted for that awesome tool!
gnome
Hi Patrick, thanks for showing jsfiddle, awsome indeed. I just updated the code to show the problem.. with 2 fields it shows both divs.
FFish
http://jsfiddle.net/7wxZy/
FFish
@FFish - I updated your jsFiddle example. http://jsfiddle.net/7wxZy/4/ You should use `.prev()` instead of `.siblings()`, since `.siblings()` will match *all* siblings, whereas `.prev()` will only look at the previous sibling. I'll update my answer.
patrick dw
Cheers Patrick, this is even better than Felix solution as I have more CSS control over the DIV's. Here is my final example: http://jsfiddle.net/7wxZy/29/
FFish
@FFish: You should still try to generate valid HTML.
Felix Kling
@FFish - Looks good, but @Felix is right. You shouldn't have `block` elements inside of `inline` elements.
patrick dw
+1  A: 

Ok, now I see the problem. Your HTML markup is wrong. You cannot have a div element inside label, use span instead:

<label for="login">User name:
    <br />
    <span class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</span>
    <br />
    <input type="text" name="login" class="input-field" value="">
</label>  

or wrap the whole block in an own div:

<div>
    <label for="login">User name:</label>
    <div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</siv>
    <input type="text" name="login" class="input-field" value="">
</div> 

Otherwise the browser will correct the markup producing this:

<label for="login">
    User name:<br />
</label> 
<div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</div>
<input type="text" name="login" class="input-field" value="">

So every label-info and input element will be on the same level and therefore siblings to each other.

Felix Kling
Felix, I have an example with 2 fields and your correction here: http://jsfiddle.net/7wxZy/ But still the same problem??
FFish
@FFish: No, you did it wrong. Either change the `div` to `span` but still have the `label` tag around it **or** keep the `div` and put another `div` around the the `label`, `div` and `input`. You are missing this "wrapping" `div` in your example. It should be more like this: http://jsfiddle.net/7wxZy/6/
Felix Kling
Sorry reading a little better, you are right.. Thanks Felix!!
FFish
@FFish: You're welcome :) Happy coding!
Felix Kling