tags:

views:

54

answers:

1

Hi,

How to change the options in a Dropdown box using JQuery.

For Eg.

 1. <select name="fieldSize" id="fieldSize">
<option>Choose a size </option>
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
 </select>

I want to change the options as

2.   <select id="fieldSize" name="fieldSize">
   <option selected="selected" value="phone" id="fieldPhoneAmerican">### - ### - ####</option>
   <option value="europhone" id="fieldPhoneEuro">International</option>
</select>

Like i want to hide the select marked as 1 and to make 2 to appear in this place .How can i do so.

+1  A: 

You be able to do the following:

$('#fieldSize').html('<option selected="selected" value="phone" id="fieldPhoneAmerican">### - ### - ####</option><option value="europhone" id="fieldPhoneEuro">International</option>');

This will remove all of the options from the select and add the 2 new ones.

Darryl Hein
You wouldn't need to `empty()` first, as `html()` will replace whatever is inside.
peirix
Good point. Changed. Just wanted to make it all proper like.
Darryl Hein