0

Ok, so I know that given the following code, pressing the button in either div will change the background color of all divs with the class of "section" to yellow. My question is, how can I change this so that it only applies to the div the button is contained in?

<script>
$(document).ready(function(){
    $(".section button").click(function(){
    $(".section").css("background-color","yellow");
});
});
</script>

<div class="section">
   <h3>Web Development</h3>
   <p>Description of web development services.</p>
   <button>Click Me</button>
</div>
    
<div class="section">
   <h3>Graphic Design</h3>
   <p>Description of graphic design services.</p>
   <button>Click Me</button>
</div>

1 Answer 1

0

Figured it out:

$(this).closest(".section").css("background-color","yellow");

Not the answer you're looking for? Browse other questions tagged or ask your own question.