tags:

views:

62

answers:

2
<html>
 <head>
 </style>
  <script type="text/javascript">

   function ram(){
   document.write("Hello World!")
   alert("ok");
   }
  </script>
 </head>
 <body>

 <select id="country">
 <option value="India" id="101" onfocus="ram()">India</option>
 <option value="Autralia" id="102" onClick="ram();">Autralia</option>
 <option value="England" id="103" onfocus="ram();">England</option>
 <option value="Ameriaca" id="104" onfocus="ram();">Ameriaca</option>
<option value="Pakistan" selected="selected" id="105" onfocus="ram();" >Pakistan</option>
 </select>
 </body>
</html>

In this above code the event will not fire for dropdown. I try by using focus and click event. I can I do this and I want 1 more thing if I select dropdown value as India, I want to create a dropdown with state (elements are TN, DL, etc) updation.

+4  A: 

You want the onchange event on the <select> tag.

Dean Harding
And then you can get the value of the selected `<option>` from `document.getElementById('country').value`.
Max Shawabkeh
to generalize it would be better to define ram with a parameter `function ram(elem)` call it with `ram(this)` from the change event and use `elem.value` in the function .. more choices that way ...
Gaby
Also note that IE does not support **focus, click, or ANY** events on option elements so attempting to do so will fail in IE.
scunliffe
A: 

Note the Question changed completely while preparing the answer

There is a synatax error in your script

function ram(){ document.write("Hello World!") alert("ok"); }

You are missing a semi-colon between the document.write() and alert()

FacilityDerek
JS has automatic semicolon insertion that would catch that case, though one certainly shouldn't rely on it.
Max Shawabkeh