I am working on a search with JavaScript. I would use a form, but it messes up something else on my page. I have this input text field:
<input name="searchTxt" type="text" maxlength="512"/> And this is my JavaScript code:
<script type="text/javascript"> function searchURL(){ window.location = "" + (input text value); } </script> How do I get the value from the text field into JavaScript?
015 Answers
There are various methods to get an input textbox value directly (without wrapping the input element inside a form element):
Method 1:
document.getElementById('textbox_id').valueto get the value of desired boxFor example,
document.getElementById("searchTxt").value;
Note: Method 2,3,4 and 6 returns a collection of elements, so use [whole_number] to get the desired occurrence. For the first element, use [0], for the second one use 1, and so on...
Method 2:
Use
document.getElementsByClassName('class_name')[whole_number].valuewhich returns a Live HTMLCollectionFor example,
document.getElementsByClassName("searchField")[0].value;if this is the first textbox in your page.
Method 3:
Use
document.getElementsByTagName('tag_name')[whole_number].valuewhich also returns a live HTMLCollectionFor example,
document.getElementsByTagName("input")[0].value;, if this is the first textbox in your page.
Method 4:
document.getElementsByName('name')[whole_number].valuewhich also >returns a live NodeListFor example,
document.getElementsByName("searchTxt")[0].value;if this is the first textbox with name 'searchtext' in your page.
Method 5:
Use the powerful
document.querySelector('selector').valuewhich uses a CSS selector to select the elementFor example,
document.querySelector('#searchTxt').value;selected by id
document.querySelector('.searchField').value;selected by class
document.querySelector('input').value;selected by tagname
document.querySelector('[name="searchTxt"]').value;selected by name
Method 6:
document.querySelectorAll('selector')[whole_number].valuewhich also uses a CSS selector to select elements, but it returns all elements with that selector as a static Nodelist.For example,
document.querySelectorAll('#searchTxt')[0].value;selected by id
document.querySelectorAll('.searchField')[0].value;selected by class
document.querySelectorAll('input')[0].value;selected by tagname
document.querySelectorAll('[name="searchTxt"]')[0].value;selected by name
Support
Browser Method1 Method2 Method3 Method4 Method5/6 IE6 Y(Buggy) N Y Y(Buggy) N IE7 Y(Buggy) N Y Y(Buggy) N IE8 Y N Y Y(Buggy) Y IE9 Y Y Y Y(Buggy) Y IE10 Y Y Y Y Y FF3.0 Y Y Y Y N IE=Internet Explorer FF3.5/FF3.6 Y Y Y Y Y FF=Mozilla Firefox FF4b1 Y Y Y Y Y GC=Google Chrome GC4/GC5 Y Y Y Y Y Y=YES,N=NO Safari4/Safari5 Y Y Y Y Y Opera10.10/ Opera10.53/ Y Y Y Y(Buggy) Y Opera10.60 Opera 12 Y Y Y Y Y Useful links
- To see the support of these methods with all the bugs including more details click here
- Difference Between Static collections and Live collections click Here
- Difference Between NodeList and HTMLCollection click Here
//creates a listener for when you press a key window.onkeyup = keyup; //creates a global Javascript variable var inputTextValue; function keyup(e) { //setting your input text to the global Javascript Variable for every key press inputTextValue = e.target.value; //listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box if (e.keyCode == 13) { window.location = "" + inputTextValue; } } See this functioning in codepen.
1I would create a variable to store the input like this:
var input = document.getElementById("input_id").value; And then I would just use the variable to add the input value to the string.
= "Your string" + input;
You should be able to type:
var input = document.getElementById("searchTxt"); function searchURL() { window.location = "" + input.value; }<input name="searchTxt" type="text" maxlength="512"/>I'm sure there are better ways to do this, but this one seems to work across all browsers, and it requires minimal understanding of JavaScript to make, improve, and edit.
Also you can, call by tags names, like this: form_name.input_name.value; So you will have the specific value of determined input in a specific form.
<input type="text" onkeyup="trackChange(this.value)"> <script> function trackChange(value) { window.open("" + value) } </script> Short
You can read value by searchTxt.value
function searchURL() { let txt = searchTxt.value; console.log(txt); // window.location = "" + txt; ... } document.querySelector('.search').addEventListener("click", ()=>searchURL());<input name="searchTxt" type="text" maxlength="512"/> <button>Search</button>1Tested in Chrome and Firefox:
Get value by element id:
<input type="text" maxlength="512"/> <input type="button" value="Get Value" onclick="alert(searchTxt.value)"> Set value in form element:
<form name="calc"> <input type="text" name="input"> <input type="button" value="Set Value" onclick="calc.input.value='Set Value'"> </form> Also have a look at a JavaScript calculator implementation:
UPDATE from @bugwheels94: when using this method be aware of this issue.
You can use onkeyup when you have more input field. Suppose you have four or input.then document.getElementById('something').value is annoying. we need to write 4 lines to fetch value of input field.
So, you can create a function that store value in object on keyup or keydown event.
Example :
<div> <div> <label for="">Name</label> <input type="text" name="fname" onkeyup=handleInput(this)> </div> <div> <label for="">Age</label> <input type="number" name="age" onkeyup=handleInput(this)> </div> <div> <label for="">Email</label> <input type="text" name="email" onkeyup=handleInput(this)> </div> <div> <label for="">Mobile</label> <input type="number" name="mobile" onkeyup=handleInput(this)> </div> <div> <button onclick=submitData()>Submit</button> </div> </div> javascript :
<script> const data={ }; function handleInput(e){ data[e.name] = e.value; } function submitData(){ console.log(data.fname); //get first name from object console.log(data); //return object } </script> If your input is in a form and you want to get value after submit you can do like
<form onsubmit="submitLoginForm(event)"> <input type="text" name="name"> <input type="password" name="password"> <input type="submit" value="Login"> </form> <script type="text/javascript"> function submitLoginForm(event){ event.preventDefault(); console.log(event.target['name'].value); console.log(event.target['password'].value); } </script> Benefit of this way: Example your page have 2 form for input sender and receiver information.
If you don't use form for get value then
- You can set 2 different id(or tag or name ...) for each field like sender-name and receiver-name, sender-address and receiver-address, ...
- If you set same value for 2 input, then after getElementsByName (or getElementsByTagName ...) you need to remember 0 or 1 is sender or receiver. Later if you change the order of 2 form in html, you need to check this code again
If you use form, then you can use name, address, ...
function handleValueChange() { var y = document.getElementById('textbox_id').value; var x = document.getElementById('result'); x.innerHTML = y; } function changeTextarea() { var a = document.getElementById('text-area').value; var b = document.getElementById('text-area-result'); b.innerHTML = a; }input { padding: 5px; } p { white-space: pre; }<input type="text" placeholder="Enter string here..." oninput="handleValueChange()"> <p></p> <textarea name="" cols="20" rows="5" oninput="changeTextarea()"></textarea> <p></p><input > <button onselect="myFunction()">it</button> <script> function myFunction() { document.getElementById("new").value = "a"; } </script> One can use the form.elements to get all elements in a form. If an element has id it can be found with .namedItem("id"). Example:
var myForm = document.getElementById("form1"); var text = myForm.elements.namedItem("searchTxt").value; var url = "" + text; Source: w3schools
simple js
function copytext(text) { var textField = document.createElement('textarea'); textField.innerText = text; document.body.appendChild(textField); textField.select(); document.execCommand('copy'); textField.remove(); } function searchURL() { window.location = ' + searchTxt.value } So basically searchTxt.value will return the value of the input field with id='searchTxt'.