Skip to content
Advertisement

Splitting sentence into words

I am basically trying to split entire input into words and then display it (possibly store it inside variables or an array so I can display it later). So far I made a system to count the words but I have to also split them so I can use them. Words are separated by space ” “

$(document).ready(function() {
    $("form").on("submit", function (e) {

        var words = $.trim($("#command_text").val()).split(' '); /* Count words*/
        alert(words.length);

        $("#command_text").val('');

        e.preventDefault();
    });
});

Answer

Add this to your code and you will see that you have already stored them into an array as DelightedDOD states.

alert(words[0]);
alert(words[1]);
alert(words[2]);

Once you have done that, you may want to close the question.

Advertisement