You can obtain the value of a text input field in JavaScript using these methods:
document.getElementById('input_id').value;
document.getElementsByClassName('class_name')[0].value;
document.getElementsByTagName('input')[0].value;
document.getElementsByName('input_name')[0].value;
By ID:
document.querySelector('#input_id').value;
By Class:
document.querySelector('.class_name').value;
By Tag Name:
document.querySelector('input').value;
Similar to querySelector, but returns a static NodeList:
By ID:
document.querySelectorAll('#input_id')[0].value;
By Class:
document.querySelectorAll('.class_name')[0].value;
By Tag Name:
document.querySelectorAll('input')[0].value;
To get the value of the search text input field in your code, use:
const searchTxtValue = document.getElementById('searchTxt').value;
Browser | Method 1 | Method 2 | Method 3 | Method 4 | Method 5/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 |
FF3.5/FF3.6 | Y | Y | Y | Y | Y |
FF4b1 | Y | Y | Y | Y | Y |
GC4/GC5 | Y | Y | Y | Y | Y |
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 |
The above is the detailed content of How to Retrieve the Value of a Text Input Field Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!