Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 1.43 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. need some advice on using getElementsByTagName
  2. <script language="JavaScript">
  3. <!--
  4.    function process(){
  5.      var a = document.getElementById('mySelect');
  6.      var res = a.options[a.selectedIndex].text;
  7.  
  8.      alert(res);
  9.    }
  10. //-->
  11. </script>
  12.        
  13. <body>
  14. <select name=""id="mySelect" onchange="process()">
  15.     <option>1</option>
  16.     <option>2</option>
  17.     <option>3</option>
  18.     <option>4</option>
  19.     <option>5</option>
  20. </select>
  21.        
  22. function process(){
  23.     var sel = document.getElementById("mySelect");
  24.     var opts = sel.getElementsByTagName("option");
  25.     for(var i=0;i<opts.length;i++){
  26.         if(opts[i].selected){
  27.              alert(opts[i].innerHTML);
  28.              break;
  29.         }
  30.     }
  31. }
  32.        
  33. function process(){
  34.     var sels = document.getElementsByTagName("select");
  35.     for(var i=0; i<sels.length;i++){
  36.         var opts = sels[i].getElementsByTagName("option");
  37.         for(var j=0;j<opts.length;j++){
  38.             if(opts[j].selected){
  39.                  alert(opts[j].innerHTML);
  40.                  break;
  41.             }
  42.         }
  43.     }
  44. }
  45.        
  46. function process(){
  47.     var sels = document.getElementsByTagName("select");
  48.     for(var i=0; i<sels.length;i++){
  49.         var sel = sels[i];
  50.         alert( sel.options[sel.selectedIndex].text );
  51.     }
  52. }
  53.        
  54. <script language="JavaScript">
  55. <!--
  56.    function process(){
  57.      var a = document.getElementsByTagName('select');
  58.      var res = a[0].options[a[0].selectedIndex].text;
  59.  
  60.      alert(res);
  61.    }
  62. //-->
  63. </script>