f



How do I get corresponding option field for selected text in html select

I have an array called voyageArr where I have the two fields Vesselid and 
Vesselname.
I want to add VesselName as display field and Vesselid as the corresponding 
field for selected VesselName

Here is the html seelct control
<select id="DDLVesselName">
</select>

Here is how I add data to the html select control. This works fine the 
Vesselname is displayed in the html select control.
function Display(flag)
{
       for (var i = 0; i < voyageArr.length; i++)
       {
             $('#DDLVesselName').append($("<option value=" + 
voyageArr[i].Vesselid + "></option>").text(voyageArr[i].VesselName));
       }
}


Here is how I get the display field
var ship = $("#DDLVesselName option:selected").text();

How do I write if I want the corresponding option field which is Vesselid 
for selected VesselName

//Tony


0
5/21/2013 8:08:10 AM
comp.lang.javascript 38369 articles. 2 followers. javascript4 (1315) is leader. Post Follow

1 Replies
745 Views

Similar Articles

[PageSpeed] 26


"Tony"  wrote in message news:knf9po$co1$1@dont-email.me...

I have an array called voyageArr where I have the two fields Vesselid and
Vesselname.
I want to add VesselName as display field and Vesselid as the corresponding
field for selected VesselName

Here is the html seelct control
<select id="DDLVesselName">
</select>

Here is how I add data to the html select control. This works fine the
Vesselname is displayed in the html select control.
function Display(flag)
{
       for (var i = 0; i < voyageArr.length; i++)
       {
             $('#DDLVesselName').append($("<option value=" +
voyageArr[i].Vesselid + "></option>").text(voyageArr[i].VesselName));
       }
}


Here is how I get the display field
var ship = $("#DDLVesselName option:selected").text();

How do I write if I want the corresponding option field which is Vesselid
for selected VesselName

//Tony

Here is how I solved my problem.
var select = document.getElementById("DDLVesselName");

           if (select.options.length > 0)
           {
              window.alert("Text: " + 
select.options[select.selectedIndex].text + "\nValue: " + 
select.options[select.selectedIndex].value);
           }
           else
           {
              window.alert("Select box is empty");
           }

0
5/21/2013 8:58:09 AM
Reply: