Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0


I have an array of objects having some questions and answers. The jQuery code shows the values of array outside the input fields but not inside it.
I want something like :
<pre>$('#input').append(this.question + "<br>");




What I have tried:

<body>
<div id="container">
    <h1> Press 'Enter' after writing your answer. </h1>
    <input type="text" id="input" name="questions[]" />
    <input type="text" id="output" />
    <input type="text" id="input" name="questions[]" />
    <input type="text" id="output" />
    <input type="text" id="input" name="questions[]" />
    <input type="text" id="output" />
    <input type="text" id="input" name="questions[]" />
    <input type="text" id="output" />
    <input type="text" id="input" name="questions[]" />
    <input type="text" id="output" />
    <a id="sub" href="#"> Submit </a>
    <div id="result"> </div>
</div>
</body>


<script>

$('document').ready(function() {

    let questions = [
        { 
            question: "1. I like tea.",
            answer: "I do not like tea."
        },

        {
            question: "2. You are my friend.",
            answer: "You are not my friend."
        },

        {
            question: "3. She is very naughty.",
            answer: "She is not very naughty"

        },

        {
            question: "4. You can do it.",
            answer: "You cannot do it"

        },

        {
            question: "5. They are good.",
            answer: "They are not good."

        },

    ];

    $.each(questions, function() {

        $('#result').append(this.question + "<br>");

    });

 });

</script>
Posted
Comments
[no name] 9-Apr-19 11:24am    
I think the point of "names" (and id's?) is that they are unique.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900