tags:

views:

51

answers:

3

Here is my HTML:

                    <li>
                        <div class="menu_inner">
                            <a href="#">
                                <div class="button"><img class="486" src="images/portalbutton.png" /></div>
                                <div class="prod_description">&nbsp;</div>
                            </a>
                        </div>
                    </li>

I want to add a .click() function to .prod_description, the click event should take the background colour applied in CSS from the li element.

Using this code:

$(".prod_description").mousedown(function() {
    $('#toolbar').css('background-color', $(this).parent().css('background-color'))
})

I dont seem to be able to get the correct $(this).parent() combination....

+3  A: 

You can do that like this:

$(".prod_description").mousedown(function() {
    $('#toolbar').css('background-color', $(this).closest('li').css('background-color'))
});

.parent() gets the immediate parent, you want to go up a few levels (<a>, <div>, <li>). .closest('li') climbs the parents and gets the first one that's an <li>. In this case, .parent('li') would also work :)

Nick Craver
A quick profiling in some of my code implies .parents('li') is faster than .closest('li'), could someone confirm/disprove this? (Assuming you only have one parent li, of course.)
David Andersson
@David - `.parents()` has different behavior all-together, it goes to the root of the DOM grabbing all parents, then matches the selector against that set. It can be faster (depending how far away the `.closest()` match is), however `.parents()` gets slower and slower the deeper your element is nested, so it's only faster for a "shallow" DOM I guess is the way to describe it. But to be equivalent, you'd have to further filter: `.parents('li:first')`, and even that's not equivalent if you're already on what you're looking for, which `.parent()` and `.parents()` won't find.
Nick Craver
@Nick Craver: thanks
David Andersson
+1  A: 

In javascript, you must use backgroundColor, not background-color.

Update: As it turns out, jQuery is able to handle both formats, and even the DOM API itself let you do something like this: obj.style.setProperty('background-color', 'red');

jholster
He is using the jQuery css ( http://api.jquery.com/css/ ) function which takes a parameter that is the actual name of the CSS property.
TommyA
Thanks for info, never realized jQuery is able to translate it.
jholster
A: 

Use click event instead of mousedown. Use closest to get the li element

$("div.prod_description").click(function() {
    $('#toolbar').css('background-color', $(this).closest('li').css('background-color'))
});

I have used a tag selector also in the click event. If you can give the background color in a CSS class, then you can use something like this

li.mybg { background-color: #a9a9a9; }

<li class="mybg">
   <div class="menu_inner">
      <a href="#">
         <div class="button">
             <img class="486" src="images/portalbutton.png" />
         </div>
         <div class="prod_description">&nbsp;</div>
      </a>
   </div>
 </li>

$("div.prod_description").click(function() {
    $('#toolbar').addClass('mybg');
});
rahul
I also first recommended using click(), until I realized click() fires after releasing mouse button (AFAIK), which may not be the desired behavior in this case.
jholster