tags:

views:

28564

answers:

5

I have the following snippet in one of my html pages :

<div class="inputboximage">
    <div class="value2">
    <input name='address1' value='Somewhere' type="text" size="26" maxlength="40" />
    <br />

    </div>
</div>

My problem is that I need the inputboximage background to change when I click in the address1 text field and to revert to the original when it loses focus.

I have used the following :

  <script>
  $(document).ready(function(){

    $("input").focus(function () {
         $(this.parentNode).css('background-image', 'url(images/curvedinputblue.gif)');
    });

    $("input").blur(function () {
    $(this.parentNode).css('background-image', 'url(images/curvedinput.gif)');
    });

  });
  </script>

but instead of replacing the image, it seems to be adding a background image to the value2 div as you would expect. I can use parentNode.parentNode in this case, but there is also a chance that the inputboxImage node could be further up or down the parent tree.

Is there a way I can change this code so that it will navigate down the parent tree until it finds a div called inputboximage and replace the image there?

Also, if I have two different div classes, inputboximage and inputboximageLarge, is there a way to modify this function so that it will work with both, replacing the background image with a different image for each one?

+16  A: 

I think using

$(this).parents('div.inputBoxImage').css(...)

instead of $(this.parentNode) should work.

See the jQuery traversing documentation

Edit: updated following Prody's answer (changed parent to parents)

Phill Sacre
Thanks Phil, this works perfectly.All I need to do now is figure out how to make it work for other elements which need the same functionality but different background images.
+4  A: 

I'm not 100% sure but I think what you need is what Phill Sacre's answer suggests except using parents (notice the last s)

From jQuery API:

parent( String expr ) returns jQuery Get a set of elements containing the unique parents of the matched set of elements.

parents( String expr ) returns jQuery Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).

Prody
You're dead right. parent(...) only searches the immediate parent. I always manage to get those two confused!
Phill Sacre
A: 

Now since in HTML only IDs are unique, you can reference the div directly without doing any traversal:

<div class="inputboximage" id="inputboximage">
    <div class="value2">
    <input name='address1'      value='5 The Laurels' type="text" size="26" maxlength="40" />
    <br />

    </div>
</div>

<script>
  $(document).ready(function(){

    $("input").focus(function () {
         $('#inputboximage').css('background-image', 'url(images/curvedinputblue.gif)');
    });


  });
</script>

Note that while it is possible to filter the parent elements for CSS classing, using style classes dor behaviour is a Bad Idea TM and you should avoid doing that. But if you are really desperate, you can give it a try:

$("input").focus(function () {
         $(this).parents('div.inputboximage').css('background-image', 'url(images/curvedinputblue.gif)');
    });
DrJokepu
A: 

.parent().css(changes here)

adardesign
A: 

i have an image tag inside a div with class and id. I have been trying to highlight another div(also a class and an id assigned) when clicking on the image div. Tried animate effect but not what I wanted. The effect should be like a flash coming and going with a background color. Can anybody help me out with this, plz?

mandeep