Running try catch using addEventListener

hi, my problem is when I am trying to run my try-catch code using addEventListener, it doesn’t work and I don’t know why
Html code:

<input type="text" id="myName">
    <button type="submit" id="subBtn">submit</button>
    <p id="text"></p>

Javascript code:

let submitBtn = document.getElementById('subBtn');

submitBtn.addEventListener('click', () => {
    let nameInput = document.getElementById('myName').value;
     let msg = document.getElementById('text');

    try {
        if(nameInput == "") throw "Empty"
    }
    catch(e) {
       msg.innerHTML = "Error is: " + e ;
    }
})

Thanks

Checked on codepen. Working as intended.

thanks, I’ve solved the problem

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.