jQuery: Change options of select using jQuery
jQuery Practical exercise Part - I : Exercise-48
Change options of select using jQuery.
Sample Solution:
HTML Code :
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Change options of <select> using jQuery.</title>
</head>
<body>
<select>
<option id="c1" value="red">Red</option>
<option id="c2" value="green">Green</option>
<option id="c3" value="white">White</option>
<option id="c4" value="blue">Blue</option>
</select>
</body>
</html>
JavaScript Code:
var option = $('<select></select>').attr("red", "orange").text("Orange");
$("#c3").empty().append(option);
See the Pen jquery-practical-exercise-48 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: Find the total width of an element (including width, padding, and border) in jQuery.
Next: Access HTML form data using jQuery.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics