I'm dealing with a scenario where multiple divs share the same class. There is a dropdown that alters the background color of one of these divs when a certain option is selected (for example, option 2 changes div #2's background color).
My dilemma lies in how to change the dropdown option when clicking on a specific div. While I understand the process of changing both the dropdown option and div upon click events, I am struggling to pinpoint exactly which div was clicked. Since the divs are generated dynamically and lack unique IDs, I can only rely on their class names.
Is there a way to determine which div was clicked relative to the entire list of divs sharing the same class?
Thanks for any guidance provided.
Example snippet:
<select class="size form-control" id="size" name="size">
<option value="1">first div</option>
<option value="2">second div</option>
<option value="4">third div..</option>
</select>
<div class="collection">random</div>
<div class="collection">text</div>
<div class="collection">inside</div>
<div class="collection">here</div>
Edit:
Current status:
Dropdown triggers color change in div based on selection.
Clicking a div results in its own color alteration.
Objective:
Clicking a div should also update the dropdown selection.
I hope this explanation clarifies the situation.