Mangooxx
Mangooxx

Reputation: 1423

Fill a JSON Array Dynamic

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>

Upvotes: 1

Views: 163

Answers (2)

Abrab
Abrab

Reputation: 831

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"]);
}

Upvotes: 1

yuk
yuk

Reputation: 933

Your code is good, just change this line:

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

http://jsfiddle.net/ajpohzv3/

Upvotes: 1

Related Questions