admin管理员组文章数量:1401384
I have a java script function
function findInthePage(str)
{
// code
}
How can I pass the contents of a HTML text box to the above function? My button click code is
<input id="Text1" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="findInthePage(text1.value);" />
I have a java script function
function findInthePage(str)
{
// code
}
How can I pass the contents of a HTML text box to the above function? My button click code is
<input id="Text1" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="findInthePage(text1.value);" />
Share
Improve this question
asked Feb 4, 2014 at 6:36
DannyDanny
31 silver badge2 bronze badges
1
- 2 Safer (and easier) to get the value from inside the function since you dont need to worry about presence of quotes etc. in the value. – techfoobar Commented Feb 4, 2014 at 6:38
7 Answers
Reset to default 3Try this:
var text = document.getElementById('Text1').value
The value property is used with Form elements, it gets or sets values to fields of the form.
To use "getElementById('id')" with form elements, they must have assigned an "id". Here's a simple example that displays an Alert with the text written in a text box.
function test7() { var val7 = document.getElementById("ex7").value; alert(val7); } html
// input type="text" id="ex7"
// input type="button" value="Click" onclick="test7()"
You can do:
var textBox = document.getElementById("Text1");
findInthePage(textBox.value);
You can access input directly from the function like:
function findInthePage()
{
// code
var input = document.getElementById('Button1').value;
}
If you want your way:
<input id="Text1" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="findInthePage(document.getElementById('Button1').value);" />
Try this
var value = null;
document.getElementById("Button1").onclick = function(){
value = document.getElementById("Text1").value;
};
Try:
<input id="Button1" type="button" value="button" onClick="findInthePage(document.getElementById('Text1').value)" />
<input id="txtbox" type="text" name="tb1" />
<input id="Button1" type="button" value="button" onclick="clickme();"/>
script
function clickme() {
var aa = document.getElementById("txtbox").value;
alert(aa);
}
版权声明:本文标题:How can I pass value(contents) of HTML textbox to a javascript function on a button click? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744230079a2596286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论