Reputation: 3
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);" />
Upvotes: 0
Views: 2884
Reputation: 197
<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);
}
Upvotes: 1
Reputation: 585
Try:
<input id="Button1" type="button" value="button" onClick="findInthePage(document.getElementById('Text1').value)" />
Upvotes: 1
Reputation: 370
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()"
Upvotes: 2
Reputation: 24354
Try this
var value = null;
document.getElementById("Button1").onclick = function(){
value = document.getElementById("Text1").value;
};
Upvotes: 1
Reputation: 988
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);" />
Upvotes: 1
Reputation: 30136
You can do:
var textBox = document.getElementById("Text1");
findInthePage(textBox.value);
Upvotes: 1