Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a JavaScript function onchange of a select tag?

Below is my JavaScript:

<script language="JavaScript">
function checkit()
{  
var state=document.frm.getElementById(stateS)
if (state="Please select a state")
    {
    document.frm.Month.disable = true;
    }
else
    {
    document.frm.Month.enable = true;
    }   
}</script>

and I call checkit() function in it from below <select> tag:

<td align="left"><g:select name="Month" from="${month.values()}" optionValue="${Month}" onChange=checkit()/></td>
 </tr> 

I have a select tag for state and until I select any state the month select box should be disabled. Can anyone tell me where I went wrong?

like image 585
laxmi Avatar asked Feb 25 '23 01:02

laxmi


2 Answers

actually there is a " missing there, it should be

onChange="checkit();"
like image 64
Hussam Sibai Avatar answered Feb 26 '23 21:02

Hussam Sibai


Change the onchange to onChange="checkit(this); and then something like the below in checkit

function checkit(selectObj)
{ 
  var idx = selectObj.selectedIndex;
  document.frm.Month.disabled = idx == 0;
}
like image 44
Jonas Elfström Avatar answered Feb 26 '23 21:02

Jonas Elfström