Link to home
Start Free TrialLog in
Avatar of saturation
saturation

asked on

Don't fire onChange first time user selects an option

I'm trying to prevent the alert box in OnChange from firing when the page loads and a user selects his or her first option.  After the first one selection, I do want the alert in the onChange to fire.  This code doesn't seem to be working.  Help?

<html>
<head>
<title></title>
<script language="javascript">
function save(i) {

      if (i != 0) {
      alert("****" + i);
      }
      i=1;
}
</script>
<BODY bgcolor=edede1 CLASS="MAINBODY" LINK=#003B72 VLINK=#003B72 TOPMARGIN=0 LEFTMARGIN=0">
<form name=form>
<select name="main" size="10" style="width:250px;" onchange="save(i);">
<option value=test></option>
<option value=test2></option>
</select>            
</form>
ASKER CERTIFIED SOLUTION
Avatar of Lakio
Lakio
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tim_Heldberg
Tim_Heldberg

You never have i defined, so it wont work, try this, it will.

<html>
<head>
<title></title>
<script language="javascript">
var i=0;
function save() {

if (i!=0) {
alert("****" + i);
}
i=1;
}
</script>
<BODY bgcolor=edede1 CLASS="MAINBODY" LINK=#003B72 VLINK=#003B72 TOPMARGIN=0 LEFTMARGIN=0">
<form name=form>
<select name="main" size="10" style="width:250px;" onchange="save();">
<option value=test></option>
<option value=test2></option>
</select>
</form>
Avatar of Pravin Asar
<script language="javascript">
// Define Global Variable
var g_IsFirstTime =1;
function DoAction(value) {
   if (g_IsFirstTime) {
      g_IsFirstTime = 0;
        return;
   }
   // Do Your other actions here
   
}

</script>
<form>
      <select onChange="DoAction(this.value);">
            <option value="1"> Option 1</option>
            <option value="2"> Option 2</option>
      </select>
</form>