Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140101

How to automatically get an input value in vanilla js?

$
0
0

Is there any javascript method to automatically get the value of an input text? I am using a barcode scanner, and i want to trigger an event the moment my text field gets a value otherwise do nothing. As of now i am using, change event like this:

if(document.getElementById("item")){
    document.getElementById("item").addEventListener("change", function(){
        let item = document.getElementById("item").value;
        if(item){
            var httpRequest = new XMLHttpRequest();
            httpRequest.open('GET', "http://"+window.location.hostname+":"+window.location.port+"/api/employee/search/1/"+item);
            httpRequest.send();
            httpRequest.onreadystatechange = function() {
                if(this.readyState == 4 && this.status == 200){
                    var employee = JSON.parse(httpRequest.responseText);
                    if(typeof(employee.id) !== 'undefined'){
                        console.log(employee)
                        urlRequest = "http://"+window.location.hostname+":"+window.location.port+"/employee/"+employee.id;
                        location.replace(urlRequest);
                    }
                }
            }
        }
    });
}

but the problem with this is the user still needs to hit the Enter in the keyboard.


Viewing all articles
Browse latest Browse all 140101

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>