Danny
Danny

Reputation: 3

How can I pass value(contents) of HTML textbox to a javascript function on a button click?

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

Answers (7)

Pramod Bhoi
Pramod Bhoi

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

Bibek Maharjan
Bibek Maharjan

Reputation: 585

Try:

<input id="Button1" type="button" value="button" onClick="findInthePage(document.getElementById('Text1').value)" />

Upvotes: 1

Danyal
Danyal

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()"

enter image description here

Upvotes: 2

zzlalani
zzlalani

Reputation: 24354

Try this

var value = null;
document.getElementById("Button1").onclick = function(){
    value = document.getElementById("Text1").value;
};

Upvotes: 1

WhatisSober
WhatisSober

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

barak manos
barak manos

Reputation: 30136

You can do:

var textBox = document.getElementById("Text1");
findInthePage(textBox.value);

Upvotes: 1

candle
candle

Reputation: 2183

Try this:

var text = document.getElementById('Text1').value

Upvotes: 3

Related Questions