简体   繁体   中英

How do I create a div and give it an ID based on a changing variable?

I'm making a quiz. The user decides how many questions he wants to answer.

A function then takes this number and runs a loop to make an individual question div for each question. The quiz shows a Chinese character and the user has to pick the correct translation.

My code:

var fillInQuestion = function() {
    questionDivIdHTML = 'question' + questionNum;

    /****************************************
    How do I make this Div's ID = to questionDivIdHTML?


    // Creates question div
    $('#questionArea').append("<div class='questionDiv'></div>");
    //$('#questionArea:last-child').attr("id", questionDivIdHTML); <-- NOT WORKING


    ***************************************/

    // Sets up a choice bank.
    var choices = [];

    // choices will be chosen from this.
    var tempAnswerSet = allChoices.slice(0);

    //find random item in the database for the question
    var thisQuestion = allQuestions[Math.floor(Math.random() * allQuestions.length)];

    // add that item to choices
    choices.push(thisQuestion);

    // remove item from 'database' so it cannot be used in another question
    allQuestions.splice(allQuestions.indexOf(thisQuestion), 1);

    // remove item from tempAnswer set so it can only be one choice
    tempAnswerSet.splice(tempAnswerSet.indexOf(thisQuestion), 1);

    // add three more items from the database (incorrect items)    
    var i = 3;
    for (i; i > 0; i--) {
        var addChoice = tempAnswerSet[Math.floor(Math.random() * tempAnswerSet.length)];
        choices.push(addChoice);
        // remove the one selected each time so they cant be chosen again
        tempAnswerSet.splice(tempAnswerSet.indexOf(addChoice), 1);
        //console.log("choices length: " + choices.length);
    }
    // shuffle the array
    choices.shuffle();

    // fill in the div with choices.
    $('#questionDivIdHTML').append("Here is an question prompt:" + thisQuestion.english + "   <br>");
    $('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[0].hanyu</script>'></input>" + choices[0].hanyu + "<br>");
    $('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[1].hanyu</script>'></input> " + choices[1].hanyu + "<br>");
    $('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[2].hanyu</script>'></input> " + choices[2].hanyu + "<br>");
    $('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[3].hanyu</script>'></input> " + choices[3].hanyu + "<br>");

};

var fillOutQuiz = function() {
    for (questionAmount; questionAmount > 0; questionAmount--) {
        fillInQuestion();
        questionNum += 1;
    }
};

I've gotten this code to work, but I broke it, when trying to add the dynamic ID and loop it.

You are saying that this portion of code is not working:

$('#questionArea').append("<div class='questionDiv'></div>");
$('#questionArea:last-child').attr("id", questionDivIdHTML);

Well, it does not work because the :last-child pseudo selector is used incorrectly (see below). It should be:

$('#questionArea').append("<div class='questionDiv'></div>");
$('#questionArea > :last-child').attr("id", questionDivIdHTML);

Or better, you can rearrange your code like this:

$("<div class='questionDiv'></div>")
    .attr("id", questionDivIdHTML)
    .appendTo("#questionArea");

  • #questionArea:last-child selects an element with id = questionArea which is also the last child of its parent
  • #questionArea > :last-child selects the last child of an element with id = questionArea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM