Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this in i a variable

JavaScript
var html = "<option value="1">Official On</option><option value="2">Official Off</option><option value="3">Sick Leave</option><option value="4">Casual Leave</option><option value="5">Annual Leave</option>"


want to select specific option of this drop down html? how to update this html with specific selected option?
Posted

1 solution

To get a specific option by the value attribute, try this:
JavaScript
var html = '<option value="1">Official On</option><option value="2">Official Off</option><option value="3">Sick Leave</option><option value="4">Casual Leave</option><option value="5">Annual Leave</option>';
var options = $('<select>' + html + '</select>'); // wrap the options in a <select> and get the jQuery object
var found = options.find("[value='1']"); // replace '1' by your value
var foundText = found.text();

If you want to modify the value later, then do this:
JavaScript
found.text("Edited value here.");

After changing the value using the above way, you can get the new HTML using the .html() method on options:
JavaScript
var newHtml = options.html();

Then newHtml will be:
HTML
<option value="1">Edited value here.</option><option value="2">Official Off</option><option value="3">Sick Leave</option><option value="4">Casual Leave</option><option value="5">Annual Leave</option>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900