mercredi 17 avril 2019

How to take the user's input through a text field when somebody types a certain sentence?

So, I put a text field that is supposed to take commands from the user, kind of like a virtual assistant. When the user types in "My name is (insert a name here)" I want the variable userName to get what's written after "My name is..." I've looked through the Internet for solutions but no use... It keeps alerting me "idk".

var userName = "";
var userResponse = document.getElementById("virtual_assistant_body_form_input");
var userResponseField = document.getElementById("virtual_assistant_body_form_input_text_message_field").value; // Selects the field element from the form element.
userResponse.onsubmit = function() { 
    if (userResponseField.includes('My name is') === true) {
        var userName = userResponseField.substr(9, 999);
        alert(userName);
    }
    else {
        alert("idk");
    }
};

Aucun commentaire:

Enregistrer un commentaire