Before using this code snippet, there are a few important considerations to keep in mind:
1- When will this code be triggered? Typically within a click eventListener, as shown in the example below:
2- What specific value do you intend to post? Please provide more details.
document.getElementById("myButton").addEventListener("click", checkInputs);
function checkInputs() {
if (document.getElementById("01").value == ""){
var newValue = document.getElementById("02").value;
document.getElementById("demo").innerHTML=newValue;
}
}
https://jsfiddle.net/azwt542p/2/
There are various methods to directly retrieve input textbox values without enclosing the input element within a form element:
These methods return a collection of elements, so use [whole_number] to specify the desired occurrence - [0] for the first element, 1 for the second one, and so forth...
Alternative 1:
Utilize
document.getElementsByClassName('class_name')[whole_number].value
to attain a Live HTMLCollection
Example:
document.getElementsByClassName("searchField")[0].value
; if this is
the initial textbox on your page.
Alternative 2:
Use
document.getElementsByTagName('tag_name')[whole_number].value
which also yields a live HTMLCollection
Example:
document.getElementsByTagName("input")[0].value;
,if this is the
primary textbox on your page.
Alternative 3:
Employ the potent
document.querySelector('selector').value
that utilizes CSS selector to target an element
Example:
document.querySelector('#searchTxt').value;
selected by id
document.querySelector('.searchField').value;
chosen by class
document.querySelector('input').value;
targeted by tagname
document.querySelector('[name="searchTxt"]').value;
picked by name