View Single Post
Old 09-05-2012, 07:52 PM   PM User | #1
clathrun
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
clathrun is an unknown quantity at this point
Question 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">
<head>
    <title></title>
</head>
<body>
   <input type="button" id="btn1" value="A" onclick="alert(btn1.value);"/>
</body>
</html>
By using this code, I could get the value which is "A"


II:
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function showID() {
            alert(this.id);
        }
    </script>
</head>
<body>
   <input type="button" id="btn1" value="A"  onclick="showID()"/>
</body>
</html>

III:
Code:
<head>
    <title></title>
    <script type="text/javascript">
        function showIDByTagName() {
            var btn = document.getElementsByTagName("input");
            alert(btn.id);
        }
    </script>
</head>
<body>
   <input type="button" id="btn1" value="A" onclick="showIDByTagName()"/>
</body>
</html>
However, by II and III, it alerts the value of the button is "undefined", really confused, looking for some help, thank you very much
clathrun is offline   Reply With Quote