I'm trying to work on something where if you click on a specific element it will update the dropdown select to match the data attribute of the div.
For the most part, this is what I have:
HTML:
<div class="selector">
<select class="js-example-basic-single">
<option value="#sedans-10" data-select="sedan" selected>Sedans</option>
<option value="#suvs-10" data-select="suv">SUVs</option>
</select>
</div>
<div class="list__vehicles">
<div class="item--vehicle active" data-vehicle="sedan">
<h2>Sedan #1</h2>
<a class="link--select" href="#">Select This</a>
</div>
<div class="item--vehicle" data-vehicle="sedan">
<h2>Sedan #2</h2>
<a class="link--select" href="#">Select This</a>
</div>
<div class="item--vehicle" data-vehicle="suv">
<h2>SUV #1</h2>
<a class="link--select" href="#">Select This</a>
</div>
<div class="item--vehicle" data-vehicle="suv">
<h2>SUV #2</h2>
<a class="link--select" href="#">Select This</a>
</div>
</div>
JS:
var link = $('.link--select');
link.on('click', function(e){
$(this).parent().addClass('active').siblings().removeClass('active');
if ($('.active').data('vehicle') = $('.js-example-basic-single option').data('select')) {
$('.js-example-basic-single option').val('selected');
}
e.preventDefault();
});
Link to Demo (CodePen): https://codepen.io/ultraloveninja/pen/JVWQeb
Overall, I'm trying to match the data attributes between the items and the selector if item--vehicle has the class active, and if there is a match then add selected to the option value with the match data attribute.
I know what I have is incorrect, but I was trying to hash it out logically from what I was trying to get out of my head.
data-vehicleattribute and if it matched thedata-vehiclethen it will either stay the same or switch the dropdown to that specific option. (e.g. If SUV #1 or SUV #2 is clicked then SUV will show selected, but if Sedan #1 or Sedan #2 is clicked then Sedan will be selected in the drop down).