This question has already been answered:

function GetData() { // получаем индекс выбранного элемента var selind = document.getElementById("SelectMyLove").options.selectedIndex; var txt= document.getElementById("SelectMyLove").options[selind].text; var val= document.getElementById("SelectMyLove").options[selind].value; alert("Теxt= "+ txt +" " + "Value= " + val); } 
 <SELECT onChange='GetData' name="SelectMyLove" id="SelectMyLove" > <OPTION SELECTED VALUE="0">Выбираем любимый фрукт</OPTION> <OPTION VALUE="1">Абрикос</OPTION> <OPTION VALUE="2">Персик</OPTION> <OPTION VALUE="3">Слива</OPTION> <OPTION VALUE="4">Груша</OPTION> </SELECT> 
In the browser writes

Uncaught ReferenceError: GetData is not defined

Reported as a duplicate at Grundy. javascript Oct 20 '17 at 2:19 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    onChange = 'GetData ()' - C.Raf.T
  • It does not help, the same mistake - Sergey74rus
  • natural question - where is the GetData function? - Igor
  • Paste into the <script>alert(window.GetData);</script> page immediately after <script src="..."></script> . What's happening? - Igor

2 answers 2

And if you add brackets?

 function GetData() { // получаем индекс выбранного элемента var selind = document.getElementById("SelectMyLove").options.selectedIndex; var txt= document.getElementById("SelectMyLove").options[selind].text; var val= document.getElementById("SelectMyLove").options[selind].value; alert("Теxt= "+ txt +" " + "Value= " + val); } 
 <SELECT onChange='GetData()' name="SelectMyLove" id="SelectMyLove" > <OPTION SELECTED VALUE="0">Выбираем любимый фрукт</OPTION> <OPTION VALUE="1">Абрикос</OPTION> <OPTION VALUE="2">Персик</OPTION> <OPTION VALUE="3">Слива</OPTION> <OPTION VALUE="4">Груша</OPTION> </SELECT> 

  • Anyway, the error - Sergey74rus
  • so here everything works out, try executing the code. - maxkrasnov
  • And where is this feature located? - maxkrasnov
  • It is in the file which is connected to html - Sergey74rus
  • First of all, try inserting the code into the html code, into the <script></script> , if it works, it means connecting the problem file, paths, etc. - maxkrasnov

Add to your js file next to the GetData function

 $("#SelectMyLove").change(GetData); 

and remove onChange from markup.