winterheat
Aug 20th 2008, 6:35 am
The following code is on http://www.0011.com/test_js/test_without_this.html
the first input field uses alert(value) instead of alert(this.value) and it works.
the second and third input fields do not use the "inline anonymous function" but use another anonymous function... but the second one will show the value while the third one won't. I thought inline anonymous function is the same as assigning element.onclick= function() { whatever there was } isn't it? If so why would the first input field work and the third doesn't? It is true in IE 7, Firefox 3, and Safari 3. Many thanks!
<form>
<input id="text1" type="text" value="wah ha ha 1" onclick="alert(value)">
<input id="text2" type="text" value="wah ha ha 2">
<input id="text3" type="text" value="wah ha ha 3">
</form>
<script>
document.getElementById("text2").onclick = function() { alert(this.value) }
document.getElementById("text3").onclick = function() { alert(value) }
</script>
the first input field uses alert(value) instead of alert(this.value) and it works.
the second and third input fields do not use the "inline anonymous function" but use another anonymous function... but the second one will show the value while the third one won't. I thought inline anonymous function is the same as assigning element.onclick= function() { whatever there was } isn't it? If so why would the first input field work and the third doesn't? It is true in IE 7, Firefox 3, and Safari 3. Many thanks!
<form>
<input id="text1" type="text" value="wah ha ha 1" onclick="alert(value)">
<input id="text2" type="text" value="wah ha ha 2">
<input id="text3" type="text" value="wah ha ha 3">
</form>
<script>
document.getElementById("text2").onclick = function() { alert(this.value) }
document.getElementById("text3").onclick = function() { alert(value) }
</script>