jQuery get css background-color value of parent element

css, dom, html, jquery

Solution

Try `closest()` to find the next matching element up the DOM tree

var color = $(this).closest("div").css("background-color");

Problem

I want to do something a bit awkward and my pseudo code isn't working, but should explain what I'm trying to do. I have groups of 4 radio buttons, in 6 separate divs, so 24 radios in total. Each div is a particular background colour. When a radio button within a particular div is selected, I need to grab the colour of the div the selected radio button is within, and have it stored in a variable ready to apply it to an element that'll be created next... ``` $("#voucher_selection_container input:radio").change(function(){ //var color = $(this).parent("div").css("background-color"); //alert(color); $("#voucher_customisation_container").slideDown(600); //$("#voucher_customisation_container .voucher_box").css("background-color", color); }); ``` Any help would be appreciated. :) Thanks.

Original source