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
Add a ment  | 

7 Answers 7

Reset to default 3

Try 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 clickStack Overflow