0

So I have this code:

$('.submit_button').click(function() {
        var optionsArray = $(".inputID").map(function() {
            return this.value;
        }).get().join(",");

        var randomOutput = optionsArray[Math.floor(Math.random()*optionsArray.length)];

        console.log(randomOutput);

    });

What I'm trying to do upon clicking the button (.submit_button) is for it to take the user entered data (they are inputs with the class .inputID), store them in an array (which i have done and it works) and then console.log (at least for now while I'm testing) one of the inputs at random. What it currently does is just console.logs a single character instead of a whole item from the array. What am I doing wrong?

1
  • You don't want to use .join(), which gives a string instead of an array. Commented Jan 11, 2015 at 16:19

2 Answers 2

3

Remove join

    var optionsArray = $(".inputID").map(function() {
        return this.value;
    }).get();

join() method joins the elements of an array into a string, and returns the string.

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

1 Comment

It's worth mentioning that the single character is because a String is effectively an array of characters.
1

You are doing a join. So optionsArray is one single string, not an array anyomore. So optionsArray[<anything>] is basically a character.

Remove the join(",") part and it'll work.

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.