March 30, 2012
find texbox value using javascript?
Question by pradip
hear is my HTML:
<div>
<input type='text' />
<input id='mybutton' type='button' value='button'/>
</div>
I want to find text box value when button is clicked:
Answer by Starx
You can select the textbox using input[type=text]
but I do not recommend doing so as this is more of a genaralised selector and cover other textboxes too if they are present.
Instead, Give an id to the textbox, like:
<input type='text' id="mytextbox" />
Using jQuery:
$("#mybutton").click(function(){
var text = $('#mytextbox').val();
console.log(text);
});
Using javascript
textbox = document.getElementById("mytextbox");
mybutton = document.getElementById("mybutton");
mybutton.onclick = function() {
console.log(textbox.value);
};