To easily achieve this functionality, utilize the power of jquery
. Simply set up a listener for when the dropdown menu changes. If the selected value is mercedes
, then you can remove the <div>
element with the ID of remove
. Adding an ID to the <select>
tag helps prevent conflicts with other dropdown menus on the page if you choose to use multiple in the future. Alternatively, you can use:
$('select').change(function(){
to replace:
$('#cars').change(function(){
$(document).ready(function(){
// Listen for dropdown change
$('select').change(function(){
if($(this).val() === 'mercedes'){
// Remove the specified element from the DOM
$('#remove').remove();
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='cars'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option> What happens when this option is selected?
<option value="audi">Audi</option>
</select>
<div id="remove">How do I remove this div?</div>