views:

222

answers:

2

Hi - I'd like to be able to select a specific option within a dropdown list in order to manipulate it. For example,

$("#mylist > option14").replaceWith(<option value="option155">New Option</option>

How can I do this? Selecting through the child selector doens't seem to work.

Thanks.

+1  A: 
$("#mylist option").each(function(){
  if($(this).val() == "option14"){
    $(this).replaceWith('<option value="option155">New Option</option>');
  }
});

or

$("#mylist option[value=option14]").replaceWith(
  '<option value="option155">New Option</option>'
);
Scharrels
A: 

You can do with :

$("select#mylist option[value='option14']").replaceWith('<option value="option155">New Option</option>');

Try below example:

<html>
<head id="Head1" runat="server">

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <title></title>
    <script type="text/javascript">
        $(function()
        {
            $("select#mylist option[value='option14']").replaceWith('<option value="option155">New Option</option>');
        }); 

    </script>    
</head>
<body>
    <select id="mylist">
        <option value="option11">One</option>
        <option value="option12">Two</option>
        <option value="option13">Three</option>
        <option value="option14">Four</option>
    </select> 
</body>
</html>
Raghav Khunger