0

I wrote the below code; when I select India/America in the dropdown related text files with some contents, has to be read and displayed inside a div element, but am getting an error in the line xhr.send()

can anyone explain why??

<html>
<head>
<script>
function getcity()
{
    var a=document.getElementById("country");
    var b=a[a.selectedIndex].value;
    alert(b);
    var xhr=new XMLHttpRequest();
    if(b=="India")
    {
        xhr.onreadystatechange=function()
        {
            if((xhr.readystate==4)&&(xhr.status==200||xhr.status==304))
            {
                document.getElementByID("display").innerHTML=xhr.responseText;
            }
        }
        xhr.open("GET","india.txt",true);
    }
    else
    {
        xhr.onreadystatechange=function()
        {
            if((xhr.readystate==4)&&(xhr.status==200||xhr.status==304))
            {
                document.getElementByID("display").innerHTML=xhr.responseText;
            }
        }
        xhr.open("GET","america.txt",true);
    }
    xhr.send(null);
}
</script>
</head>
<body>
<select id="country" onchange="getcity()">
<option>India</option>
<option>America</option>
</select>
<div id="display"></div>
</body>
</html>
venergiac
  • 7,469
  • 2
  • 48
  • 70
user3094600
  • 29
  • 1
  • 2
  • 7

1 Answers1

0
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
function getcity()
{
    var a=document.getElementById("country");
    var b=a[a.selectedIndex].value;
    alert(b);
    var xhr=new XMLHttpRequest();
    if(b=="India")
    {
        xhr.onreadystatechange=function()
        {
            if((xhr.readystate==4)&&(xhr.status==200||xhr.status==304))
            {
                document.getElementByID("display").innerHTML=xhr.responseText;
            }
        }
        xhr.open("GET","india.txt",true);
    }
    else
    {
        xhr.onreadystatechange=function()
        {
            if((xhr.readystate==4)&&(xhr.status==200||xhr.status==304))
            {
                document.getElementByID("display").innerHTML=xhr.responseText;
            }
        }
        xhr.open("GET","america.txt",true);
    }
    xhr.send(null);
}
</script>
</head>
<body>
<select id="country" onchange="getcity()">
<option>India</option>
<option>America</option>
</select>
<div id="display"></div>
</body>
</html>