javascript – onclick check empty id val

Question:

is it possible to implement a similar code ?:

<input type text id="test"><a onclick ="if #test val empty return false">

that is, so as not to follow the link if input #test has an empty value

Answer:

document.querySelector('a').addEventListener('click', function(e) {
    if (document.getElementById('test').value.trim() === '') {
        e.preventDefault();
        // или 
        // return false;
    }
});
Scroll to Top