Select onChange without working inside the form

I have a question that submit and onchange forms do not work together. When I change the value in the drop down list, view viewroom () does not start. Can anyone help me with this? The code is as follows

<script type="text/javascript"> function viewroom() { alert(123); } </script> <form id="myform" name="myform" action="joinroom.php" method="post"> <select name="viewroom" id="viewroom" onChange="viewroom()"> <option value="1">Room1 </option> <option value="2">Room2 </option> </select> <input type="submit" onclick="this.form.submit()" value="Join room"><br> </form> 
+4
source share
3 answers

Your function name conflicts with the name and id from select , just enter a different function name.

+14
source

You cannot name a function the same as an element on your page. I suggest changing the function name to something like viewroomSelected , as shown here in this jsFiddle .

According changes:

 function viewroomSelected() { alert(123); } <select name="viewroom" id="viewroom" onChange="viewroomSelected()"> 
+4
source

I found that when you set the name and id attribute to the same for your function name, causing this conflict, and the viewroom error viewroom not a function, change the name of the function. also define your js at the bottom of the document.

 function viewRoom() { alert(123); } 

Working demo

+2
source

All Articles