I am calling a function named ‘capital()’ but it is not working and ‘capita’ is working.
is it a keyword in js or what is this ?
here is the code see it, test it
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML Select Element</title>
<script type="text/javascript">
function capita(){
alert('yes');
c = new Array("Islamabad", "Tehran", "Bejing", "New Delhi", "Kabul");
var i;
i = document.f1.country.selectedIndex;
document.f1.capital.value=c[i];
}
</script>
</head>
<body>
<h3>Countries and Capital</h3>
<form name="f1" method="post">
The capital of
<select name="country" id="country" onChange="capita();">
<option selected>Pakistan
<option>Iran
<option>China
<option>India
<option>Afghanistan
</select>
is <input type="text" name="capital" value="Islamabad"/>
</form>
</body>
</html>
capitalis a reference to your input element.This is because when you use inline handlers, there are certain DOM elements that are inserted into the variable scope chain. Exactly which elements are inserted depends on the browser to some degree.
If you change the
onchangehandler of yourselectelement to this:You’ll see that it alerts:
…or something similar.
JSFIDDLE DEMO
Here’s an updated example that changes your inline handler like this:
So now it alerts
capitalandwindow.capital. If you test this in different browsers, you’ll probably get different results.JSFIDDLE DEMO
Chrome shows: