Execute function if input contains certain text

Asked

Viewed 101 times

3

$(function() {
    if ($fa.ismod = true) {
        if (document.getElementById("input#message").value = "/msg") {
            alert('tudo okay');
        }
    }
});

This code should do an Alert if input#message contains the text /msg but it returns this:

Uncaught Typeerror: Cannot set Property 'value' of null(...)

  • The getElementById selector is incorrect and in condition if you are assigning the value "/msg" not comparing.

1 answer

4


The error showing occurred because you tried to assign value to an element that does not exist.

This will validate the value of the HTML element that has the id equal message.

if (document.getElementById("message").value === "/msg") {
    alert('tudo okay');
}

This will validate the element input HTML that has the id equal to message.

if ($("input#message").val() === "/msg") {
    alert('tudo okay');
}

Note the errors I pointed out by comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.