I'm looking to use jQuery to change the background color of a div on click (which I've got working!) However one click changes all of the divs color at that point, I'm wondering if there is any way to change the color for the specific element that has been clicked without affecting the others?
$(document).ready(function() {
$(".bg").click(function() {
$(".bg").css("background-color", "red");
});
});
.bg {
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="bg">
<p id="demo" onclick="colorSwitch()">Click me to change my text color.</p>
</div>
<div class="bg">
<p id="demo" onclick="colorSwitch()">Click me to change my text color.</p>
</div>
<div class="bg">
<p id="demo" onclick="colorSwitch()">Click me to change my text color.</p>
</div>
<p>A function is triggered when the div element is clicked. The function sets the background-color of the div element to red.</p>
Also JSFiddle is here: https://jsfiddle.net/davywest1000/jd82hq5t/2/
Thanks in advance!