0

So i was making some console stuff for my things and i wanted to make some kind of"detection" of command, like if what i input into the thing is command then make it change color of text to white

it did work if i use variable like this

var commands = ['command1'];

or

var commands = 'command1';

but i did not with variable used like in code down there

code:

var commands = ['command1', 'commands2']

window.setInterval(function() {
    if (input.innerHTML.toLowerCase() == !commands) {
        console.log("test")
    }
}, 32);

2
  • Please provide some HTML along with it. Commented Jul 15, 2021 at 16:32
  • What are you trying to do with your code at the bottom? Right now, you are checking if the string in the innerHTML is false since ! an array is always false. Commented Jul 15, 2021 at 16:38

1 Answer 1

1

You can check using indexOf. indexOf() returns -1 when the supplied value isn't present

var commands = ['command1', 'commands2']
commands.indexOf(input.innerHTML.toLowerCase()) > -1

var input = document.getElementById("cmd");
var output = document.getElementById("demo"); 

var commands = ['command1', 'commands2'];

input.addEventListener("input", function() {
    var result = null;

    // indexOf() returns -1 when the supplied value isn't present
    if(commands.indexOf(cmd.value.toLowerCase()) > -1){
      // your logic to change color, or do whatever
      result =  "yes";
    } else {
      result = "no";
    }
    output.textContent = result;

});
<input id="cmd">

<p id="demo"></p>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.