Thompson
Thompson

Reputation: 2000

How to get and then change this drop-down value to default?

I have a drop-down list as,

<select id='idleReason' onChange="loadXMLDoc(this.value)" size="1">
<option value="nothing" selected="selected">Select a site</option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>

Now I want when loadXMLDoc() javascript function to be called on change, which will have some other events in it. But I am not able to perform one thing. I want that when the value is selected from the drop down, then the event go to loadXMLDoc() function, and reset the drop-down value to nothing (which is default value).

Can someone please give me any idea to implement this in jQuery or Javascript? Thanks.

Upvotes: 0

Views: 202

Answers (2)

Shyju
Shyju

Reputation: 218732

HTML

<select id='idleReason'  size="1">
<option value="nothing" selected="selected">Select a site</option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>​

Script

$(function(){
    $("#idleReason").change(function(){
       var item=$(this); 
       if(item.val()!="nothing")
       {
           loadXMLDoc(item.val());
           item.val("nothing"); 
       }         
    });     
});

Working sample : http://jsfiddle.net/spe6B/2/

Upvotes: 2

aquinas
aquinas

Reputation: 23796

How about:

<select id='idleReason' onChange="if (this.value!='nothing') {loadXMLDoc(this.value); this.value='nothing';}" size="1">
<option value="nothing" selected="selected">Select a site</option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>​

Upvotes: 1

Related Questions