vendredi 20 mars 2020

If value exists, do this. undefined vs null?

I want to render certain action upon button click.

The condition is: (If value exists in input field, then write 'value exists'. If value doesn't exists, then alert).

I tried to replace undefined with null. I also try to replace operator === and ==. None works! Please help.

<html>
<head>

</head>
<body>

<input id='typer' type='text'></input>
<button onclick='hey()'>click</button>

<p id='pw'></p>

<script>

var pw
pw = document.querySelector('#pw');

function hey() {
    if (document.getElementById('typer').value !== undefined) {
        pw.innerHTML = 'value exists';
    } else {
        alert('value does not exists');
    }
}

</script>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire