Javascript will not pass information into function from html -
ok new javascript. trying learn code changing text on button using external javascript file. can't javascript read buttons valueexternally, in chrome's debug tools see button value btn="". reads button object can't read properties.
<html> <head> <title> test </title> <script type="text/javascript" src="gle.js"></script> </head> <body> <div><canvas id="gle" width="800" height="600"></canvas> </div> <div> <h2>enter mass , coordinates</h2> <input id="txtbox" type="text" /><br/> <button id="btn" onclick="change()">add</button> </div> </body> </html>
the gle.js
"use strict"; function change() { var x = document.getelementbyid("btn").value; var elem = document.getelementbyid("btn"); var txt = document.getelementbyid("txtbox"); txt.text = elem.value; elem.value = "ok"; }
when debug x value "", nothing changes on screen. using brackets ide.
x
empty because '#btn'
doesn't have 'value' attribute specified. "add" string inside inner html (or text),
alert(document.getelementbyid("btn").innertext);
and can index this
in event scope, it's reference of '#btn'
, this.innertext
.
a alternative '#btn'
child nodes values, cross-browser.
alert(this.childnodes[0].nodevalue);
this alert first text specified in element inner.
Comments
Post a Comment