user3871
user3871

Reputation: 12716

submit text box input using button

How do you submit text box input to a javascript function without submitting the server?

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe(value typed into text box)" id="testButton" >Submit Response</button>

Javascript:

function submitMe(input) {
    alert(input); //should output text box input
}

Thanks

Upvotes: 1

Views: 13260

Answers (2)

Abraham Hamidi
Abraham Hamidi

Reputation: 13839

Try

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe(document.getElementById('test').value)" id="testButton" >Submit Response</button>

DEMO

Upvotes: 1

xdazz
xdazz

Reputation: 160943

No need to pass by parameter, just the the element by id.

function submitMe() {
    var value = document.getElementById('test').value;
    alert(value);
}

Or you could pass the id like:

<input type="text" id="test"  value="" />
<br/>
<button onclick="submitMe('test')" id="testButton" >Submit Response</button>

js:

function submitMe(id) {
    var value = document.getElementById(id).value;
    alert(value);
}

Upvotes: 3

Related Questions