![]() |
How to obtain the value in a button?
I am new in JS, and meet a problem, asking for some suggestions, really appreciated.
The question is I don't know how to obtain the value in a button by an independent method, the codes are below: I: Code:
<html xmlns="http://www.w3.org/1999/xhtml">II: Code:
<html xmlns="http://www.w3.org/1999/xhtml">III: Code:
<head> |
1. Why are you trying to use .id if you want to access the value? Why not use .value?
2. In II: You assigned an event handler using one of the onxxxxx attributes. In that case the context for the function does not change to be a reference to the DOM object, so "this" will be a reference to the window object instead. 3. In III: getElementsByTagName (mind the 's') will return a collection of DOM objects which is essentially an array. In order to retrieve the first object from the array, you will have to use the index [0] on it Code:
<input type="button" id="btn1" value="A" onclick="showValue(this)"/> |
Code sample #1 has
alert(btn1.value); and code samples #2 and 3 have alert(this.id); and alert(btn.id);, respectively. Notice any difference? |
| All times are GMT +1. The time now is 09:38 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.