1

I have several divs making up a single button on a menu. Using jQuery, I'm trying to update the divs to a hover background image, without using individual selectors.

$(document).ready(function () {
   $(".menuitem div").on('hover', function () {
      $("div").get(1).css('background-image', 'url("img/header/leftHov.png");');
   });
});

When I run this code, I get "Object div has no method 'css'". My HTML in case you were wondering looks something like this:

<div style="width:100px;" class="menuitem">
    <div style="background: url('img/header/leftReg.png');">
    <div style="background:url('img/header/midReg.png');background-repeat:repeat-x;">
    <a href="./about.php" class="menu">About </a>
    <div style="background:url('img/header/rightReg.png');">
</div>
Jack Guy
  • 8,346
  • 8
  • 55
  • 86
  • 1
    you're approaching this incorrectly. inside the "on" function don't select all divs, just do $(this) to get the div selected by the ".menuitem div" – Liviu T. Aug 26 '12 at 21:40
  • I can't just bind it to one div since I have several divs that need updated background images. – Jack Guy Aug 26 '12 at 21:44
  • @J4G: It's `background-image`, just like you initially had, not `backgroundImage`. – João Silva Aug 26 '12 at 21:48
  • $("div").get(1) means first div on the page...if that's what you want ok, but it feels wrong. What happens if you have to add a new div before that one? – Liviu T. Aug 26 '12 at 22:08

1 Answers1

2

.get() will give you the DOM element, not the jQuery object. You should use .eq() instead. However, since you are already binding the event to the div that you want, simply use $(this):

$(this).css('background-image', 'url("img/header/leftHov.png")');
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • I can't just bind it to one div since I have several divs that need updated background images. Thanks for the .eq() tip though. – Jack Guy Aug 26 '12 at 21:44
  • @J4G: So you basically want to change the background-image of all images to leftHov.png, midHov.png, and rightHov.png instead? Or just a single one? – João Silva Aug 26 '12 at 21:45