Thursday 16 August 2012

To get title of option by Javascript

To get the value of Title of select option by JavaScript , following is the example.

<select id="myinfo" name="myinfo"  onchange="getTitle();">
<option value="">Select</option>
<option value="1" title="aa">First value</option>
<option value="2" title="bb">Second value</option> 
<option value="3" title="cc">Third value</option> 
</select>

<script>
function getTitle(){

        var mylist = document.getElementById(' myinfo ');

var myindex = mylist.selectedIndex;  
               //** 0,1,2,3....are index option, starting from 0
              //** myindex will contain numeric value of Index of selected text
var mytitle= mylist.options[myindex].title;
   
         alert(mytitle);
}
</script>

suppose if we select " Second Value " in myinfo then "bb" will get alerted by the above function.