I want to dynamically add class for this selected class.
HTML :
<select name="number" id="paragraphSpaceOPtion">
<option class="option-1" value="00">00</option>
<option class="option-2" value="05">05</option>
<option class="option-3" value="07">07</option>
<option class="option-4" value="10">10</option>
<option class="option-5" value="15">15</option>
<option class="option-6" value="20">20</option>
</select>
Here is a content I wan't to add class:
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, in!</p>
</div>
JS :
$(document).ready(function(){
var e = document.getElementById("paragraphSpaceOPtion");
var strUser = e.options[e.selectedIndex].value;
//console.log("option number " + strUser);
//var masud = typeof(strUser);
//console.log(masud);
if(strUser == "00"){
$(".content").addClass("option-no-00");
}
if(strUser == "05"){
$(".content").addClass("option-no-05");
}
if(strUser == "07"){
$(".content").addClass("option-no-07");
}
if(strUser == "10"){
$(".content").addClass("option-no-10");
}
if(strUser == "15"){
$(".content").addClass("option-no-15");
}
if(strUser == "20"){
$(".content").addClass("option-no-20");
}
})
I wan't to dynamically add class name when I select option.
Here you go with one more solution
var prevVal;
$("#paragraphSpaceOPtion").on("change",function(){
var val = $(this).find('option:selected').val();
$(".content").removeClass(`content-${prevVal}`).addClass(`content-${val}`);
prevVal = val;
});
.content-00{ color:pink}
.content-05{ color:blue}
.content-07{ color:green}
.content-10{ color:red}
.content-15{ color:yellow}
.content-20{ color:gray}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="number" id="paragraphSpaceOPtion">
<option class="option-1" value="00">00</option>
<option class="option-2" value="05">05</option>
<option class="option-3" value="07">07</option>
<option class="option-4" value="10">10</option>
<option class="option-5" value="15">15</option>
<option class="option-6" value="20">20</option>
</select>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, in!</p>
</div>
I've used ES6
template literals and used a variable to hold the previous selected value.
Hope this will help you.
$(".content").removeClass().addClass("content option-no-"+$('#paragraphSpaceOPtion').find('option:selected').val());
$("#paragraphSpaceOPtion").on("change",function(){
var val=$(this).find('option:selected').val();
$(".content").removeClass().addClass("content option-no-"+val);
});
.option-no-00{ color:pink}
.option-no-05{ color:blue}
.option-no-07{ color:green}
.option-no-10{ color:red}
.option-no-15{ color:yellow}
.option-no-20{ color:gray}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="number" id="paragraphSpaceOPtion">
<option selected class="option-1" value="00">00</option>
<option class="option-2" value="05">05</option>
<option class="option-3" value="07">07</option>
<option class="option-4" value="10">10</option>
<option class="option-5" value="15">15</option>
<option class="option-6" value="20">20</option>
</select>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, in!</p>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With