How to get value from input box in JavaScript?

By Faruque Ahamed Mollick

In this post, I am going to provide you a useful JavaScript code snippet. Here I am going to tell you how you can take the value of an input box.

To do it take an input text box for example that is given below:

<input name="searchBox" type="text" maxlength="300" id="searchText" />

The above form has the ID “searchBox”. Using this unique ID, you can retrieve the input text value.

Below is the given JavaScript code which will retrieve the input text box value:

Method 1:

document.getElementById('searchBox').value;

Method 2:

document.querySelector('#searchBox').value;

Method 3:

This method is going to use jQuery. If you are using jQuery framework, then you can get the text input value using the below jQuery code:

$('#searchBox').val();

 

Leave a Reply

Your email address will not be published. Required fields are marked *