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" id="searchTxt" class="searchField"/>
And this is my JavaScript code:
<script type="text/javascript">
function searchURL(){
window.location = "http://www.myurl.com/search/" + (input text value);
}
</script>
How do I get the value from the text field into JavaScript?
I would create a variable to store the input like this:
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:
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.
Try this one
See this functioning in codepen.
Tested in Chrome and Firefox:
Get value by element id:
Set value in form element:
https://jsfiddle.net/tuq79821/
Also have a look at a JavaScript calculator implementation: http://www.4stud.info/web-programming/samples/dhtml-calculator.html
UPDATE from @bugwheels94: when using this method be aware of this issue.
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.