views:

304

answers:

4

What I'm trying to accomplish is when a user has focus on a text box, the field set that's in will add a class "active_fieldset" so it gives a nice visual cue of where the user is in the form. Using the following javascript, it does affect the parent fieldset but it also affects all sibling fieldsets as well. Am I doing something wrong? Is there something fundamentally wrong with my HTML or javascript?

Example HTML:

<div id="content">

 <form action="next_page" method="post">

  <fieldset>
   <legend>Foo</legend>

   <p><label for="one">One</label> <input type="text" class="input_text" name="one" value="" id="one"></p>
  </fieldset>

  <fieldset>
   <legend>Bar</legend>

   <p><label for="two">Two:</label><input type="text" class="input_text" name="two" value="" id="two"></p>

  </fieldset>

  <p><input type="submit" value="Continue &rarr;"></p>
 </form>

</div>

form.js:

$(document).ready(function(){
 $('.input_text').focus(function(){
  $(this).parents('fieldset').addClass("active_fieldset");
 });
});

EDIT:

I've included my CSS:

fieldset
{
    border-width: 10px 0 0 0;
    border-style: solid;
    border-color: #0D6EB8;
}

fieldset.active_fieldset
{
    border-width: 10px 0 0 0;
    border-style: solid;
    border-color: #0D6EB8;
}
+2  A: 

Try using the closest method. You'll probably need to pair this with some more code to make sure that the class has been removed from all the other field sets.

$('.input_text').focus( function() {
    $('fieldset').removeClass('active_fieldset');
    $(this).closest('fieldset').addClass('active_fieldset');
});

Quoting from the docs:

Closest works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn't match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression. If no matching element is found then none will be returned.

tvanfosson
That is one way of doing it but doesn't actually address the problem the user has.
cletus
cletus is right, I tried closest as well and it is causing the same issue.
Kylee
I'm not sure that the problem is with the selector, but perhaps with the failure to remove the class from the other fieldsets when the focus shifts. I don't think that the parents method is actually selecting the siblings of the parent (that would be a bug IMO). I've updated my example.
tvanfosson
A: 

I'd suggest:

$("input.input_text").focus(function() {
  $("fieldset.active_fieldset").removeClass("active_fieldset");
  $(this).parents("fieldset").addClass("active_fieldset");
}).blur(function() {
  $("fieldset.active_fieldset").removeClass("active_fieldset");
});
cletus
That causes some odd behavior. I made the class "active_fieldset" to display: none; and the sibling fieldset disappears but the parent fieldset doesn't. I change "active_fieldset" to only change the text color to red and it works on only the parent fieldset. I then change "active_fieldset" to the following: border-width: 10px 0 0 0; border-style: solid; border-color: red;None of the fieldsets are affected now. Bizarre.
Kylee
You've got something else going on. Probably with your CSS.
cletus
A: 

Use parents() with parameter.

$("#test").parents('.something'); // will give you the parent that has a something class.

Elzo Valugi
I tried this as well. $(this).parents('fieldset') but this creates the same behavior as described in the question.
Kylee
A: 

Perhaps it's the CSS code that has a bug. I think the suggestion to use JQuery.closest was correct.

moell
I've edited the question to include the css I'm using.
Kylee