1

I try to fill a json array dynamically after everytime i press a button. My goal is later to safe this array in a txt file, but this is an other story and for that i found examples at W3C-Page.

this is my code so far:

enter 
<html>
<body>

 <table>
    <tr>
        <td><label>Subject: <input id="subject" type="text" /></label></td>
        <td><label>Semester: <input id="semester" type="text" /></label></td>
        <td><label>Name: <input id="name" type="text" /></label></td>
    </tr>
    <tr>
       <td>
           <label>Question: <label>
       </td>
       <td colspan="2">
            <textarea id="question" style="width:512px;height:100px"></textarea>
       </td>
   </tr>
   <tr>
       <td>
            <label>Answer 1: <label>
        </td>
        <td colspan="2">
             <textarea id="Answer1" style="width:512px;height:100px"></textarea>
        </td>
    </tr>
    <tr>
        <td>
            <label>Answer 2: <label>
        </td>
        <td colspan="2">
            <textarea id="Answer2" style="width:512px;height:100px"></textarea>
       </td>
    </tr>
    <tr>
        <td>
            <label>Answer 3: <label>
        </td>
        <td colspan="2">
            <textarea id="Answer3" style="width:512px;height:100px"> </textarea>
       </td>
    </tr>
     <tr>
        <td>
           <label>Answer 4: <label>
        </td>
        <td colspan="2">
            <textarea id="Answer4" style="width:512px;height:100px">   </textarea>
        </td>
    </tr>
    <tr>
        <td></td>
        <td><button onclick="saveInputInArray()">Save in Array</button></td>
        <td></td>
    </tr>
</table>

<script type='text/javascript'>

var quiz = {
    question:[]
};

function saveInputInArray()
{
     quiz.question.push({
        "Subject" : document.getElementById("subject").value,
        "Semester" : document.getElementById("semester").value,
        "Name" : document.getElementById("name").value,
        "Question" : document.getElementById("question").value,
        "Answer1" : document.getElementById("Answer1").value,
        "Answer2" : document.getElementById("Answer2").value,
        "Answer3" : document.getElementById("Answer3").value,
        "Answer4" : document.getElementById("Answer4").value
    });

     alert("Semester: " + quiz["question"]["semester"]);
}

</script>

</body>
</html>
1
  • 2
    you don't "fill a json array". json is a transport encoding format. you fill a javascsript array, and then encode it into json when you're done filling. Commented Jun 1, 2015 at 21:34

2 Answers 2

1

Your code is good, just change this line:

alert("Semester: " + quiz["question"][0]['Semester']);

http://jsfiddle.net/ajpohzv3/

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

Comments

1

Considering this line of code that you wrote

alert("Semester: " + quiz["question"]["semester"]);

I'd do like such :

var quiz = {
    question:[]
};

function saveInputInArray()
{

    "subject semester name question Answer1 Answer2 Answer3 Answer4".split(" ").forEach(function(id){
        quiz.question[id] = document.getElementById(id).value;
    });

    alert("Semester: " + quiz["question"]["semester"]);
}

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.