2

I am student learning jQuery and my goal is to append each object within an array to 4 different buttons using the 4 objects within the indexes of optionArray. Currently it appends just a single button of the index i of the optionArray like this: Radio Head, Gorillaz, Coldplay, Arctic Monkeys.

I would like it to append each object individually within index i of optionsArray as 4 different buttons. For example:

  1. Radio Head
  2. Gorillaz
  3. Coldplay
  4. Arctic Monkeys

I am unsure how to do this...thank you for any help in advanced.

JS:

window.onload = function() {
    $('#start').html('<div class="text-center"><button type="button" class="btn btn-default">Start</button></div>');
};

var questionArray = ["This bands second album went platinum 5 times in the UK and double Platinum in the US."];
var optionArray = [["Radio Head", "Gorillaz", "Coldplay", "Arctic Monkeys"], []];
var answerArray= ["Gorillaz"];
var imageArray= ["http://cdn3.pitchfork.com/artists/1767/m.65d9c64d.jpg", "http://crowningmusic.com/storage/rA7GUFFoBCtT8Jg4L1tv.png", "", "", ""];

var count = 0;

$("#start").on('click', function() {
    $(this).css("display","none");
    for (var i = 0; i < questionArray.length; i++) {
        $("#question").html(questionArray[i]);
        for (var j = 0; j < 4; j++) {
            $("#options").append("<button>" + optionArray[i] + "</button>");
        }
    }   
    // $("#holder").html("<img src=" + questionArray[count] + ">");
});
2
  • Your goal is hard to understand "I would like it to append each index within index i as 4 different buttons." Commented May 22, 2017 at 4:43
  • is this what you are looking for? jsfiddle.net/36zck6dk Commented May 22, 2017 at 4:46

1 Answer 1

3

You are using a 2 dimensional array for options. Change your for loop iteration of options to this

    for (var j = 0; j < 4; j++) {
       $("#options").append("<button>" + optionArray[i][j] + "</button>");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

brilliant, thank you! this is exactly what I was hoping for.

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.