I have a html form like this:
<form action="/create" method="POST">
<input type="text" placeholder="title" name="title">
<input type="text" placeholder="year" name="toYear">
<input type="text" placeholder="genre" name="genre">
<input type="text" placeholder="director" name="director">
<input type="url" placeholder="poster" name="poster">
<textarea name="plot" placeholder="plot..." cols="30" rows="10"></textarea>
<div id="actors">
<h5>Actors</h5>
<button type="button" id="addActor">Add Actor</button>
</div>
<div id="ratings">
<h5>Ratings</h5>
<button type="button" id="addRating">Add Rating</button>
</div>
<button type="submit">Add to Collection
</button>
</form>
And my script below it:
document.getElementById('addRating').addEventListener('click', function () {
var ratingsElem = document.getElementById('ratings');
var sourceElem = document.createElement("input");
sourceElem.setAttribute('type', 'text');
sourceElem.setAttribute('placeholder', 'Source');
sourceElem.setAttribute('name', 'ratingsSource');
var valueElem = document.createElement("input");
valueElem.setAttribute('type', 'text');
valueElem.setAttribute('placeholder', 'value');
valueElem.setAttribute('name', 'ratingsValue');
ratingsElem.appendChild(sourceElem);
ratingsElem.appendChild(valueElem);
})
document.getElementById('addActor').addEventListener('click', function () {
var ActorsElem = document.getElementById('actors');
var actorElem = document.createElement("input");
actorElem.setAttribute('type', 'text');
actorElem.setAttribute('placeholder', 'Name');
actorElem.setAttribute('name', 'actors');
ActorsElem.appendChild(actorElem);
});
Everything goes well on submit but I want to make the Ratings values into an array of rating objects like this:
"Ratings": [
{
"Source": "Internet Movie Database",
"Value": "8.8/10"
},
{
"Source": "Rotten Tomatoes",
"Value": "91%"
},
{
"Source": "Metacritic",
"Value": "92/100"
}
]
and append it to form data sent to server. How may I achieve this? ........................................