Write a jQuery code to remove all the options of a select box and then add one option and select it.
Write a jQuery code to remove all the options of a select box and then add one option and select it.
Answer :
File Name :
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<title>SP Creation</title>
</head>
<body>
<div class="container">
<div class="col" id="box">
<select name="select" id="sel">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
</div>
<button id="remove">Remove</button>
<button id="add">Add New</button>
</div>
<script>
$(document).ready(function(){
$("#add").click(function(){
$("select").append("<option>Item NEW</option>");
});
$("#remove").click(function(){
$("select").empty();
});
});
</script>
</body>
</html>
Write a jQuery code to remove all the options of a select box and then add one option and select it.
Reviewed by technical_saurabh
on
April 30, 2021
Rating:
No comments:
Post a Comment