I have an HTML element select # options that when selecting one of the options it will dynamically add classes to the div # container element, but I need to delete the class that was previously added to this element and then add the new class to the selected option.
If you notice, the classes are always added but, but they are still the same ones that were added.
$(function(){
$(document).on('change', 'select#options', function(){
var option_selected = $(this).val();
$('#container').addClass(option_selected);
$('#container').html(option_selected);
console.log($('#container').attr('class'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="options" id="options">
<option value="" selected>Select</option>
<option value="style1">Style 1</option>
<option value="style2">Style 2</option>
<option value="style3">Style 3</option>
<option value="style4">Style 4</option>
<option value="style5">Style 5</option>
</select>
<div id="container" class="container default"></div>
I appreciate your help.