th,td {
padding: 5px;
}
</style>
<body>
<h2>The XMLHttpRequest Object</h2>
<form action="">
<select name="customers" onchange="showCustomer(this.value)">
<option value="">Select a customer:</option>
<option value="ALFKI">Alfreds Futterkiste</option>
<option value="NORTS ">North/South</option>
<option value="WOLZA">Wolski Zajazd</option>
</select>
</form>
<br>
<div id="txtHint">Customer info will be listed here...</div>
<script>
function showCustomer(str) {
var xhttp;
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xhttp.open("GET", "getcustomer.asp?q="+str, true);
xhttp.send();
}
</script>
</body>
</html>
Hey and sorry for the basic question :) just studying some JavaScript's AJAX and I'm confused with the use of "this" in this code block, in the onchange EventHandler. It seems as if "this" refers to the option element but I can't really understand how or why.
EDIT : seems like it's not so basic.
I read the detailed answer of a general question about "this"
How does the "this" keyword work?
and also the great article :
http://www.digital-web.com/articles/scope_in_javascript/
both of them serve a great deal of people, but do not touch exactly my question.
I can't figure out what's the exact behavior of "this" when using it inside the HTML element, with a javascript function.
I hope someone understands my meaning