What is the best course of action to allow the creation/deletion of matrix fields in front-end. For context this in a Commerce Order Field. JS is mess right now, any underlying Craft JS libraries I can tap into?
Here is the whole mess below, having issues with users getting stuck here if they accidentally leave of the matrix fields empty.
{#
# 2016 Apply-3
# ---------------
#}
{% extends '_layouts/_event-checkout' %}
{% block content_inside %}
<section>
<div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1">
<div class="feature boxed bg-secondary">
<form method="post">
<input type="hidden" name="action" value="commerce/cart/updateCart">
<input type="hidden" name="redirect" value="apply/review"/>
<input type="hidden" name="paymentMethodId" value="2">
<input type="hidden" name="fields[applicationProgress]" value="2">
<input type="hidden" name="fields[applicationSchemaVersion]" value="{{ switchboard.applicationSchemaVersion }}">
<div class="text-center">
<h4 class="uppercase mt48 mt-xs-0">Great, Now we know you {{ application.shippingAddress.firstName }}!</h4>
<p class="lead mb64 mb-xs-24">
Now let's see what you know!
</p>
</div>
{{ getCsrfInput() }}
<hr class="mt16">
<div class="row mt64">
<div class="col-sm-12 input-with-label">
<span>Tell me something that's true, that almost nobody agrees with you on.</span>
<textarea name="fields[thielQuestion]"
placeholder="Some may recognize this famous question, we want some original answers though, really think about this one.">{{ application.thielQuestion }}</textarea>
</div>
<div class="col-sm-12 mt48 input-with-label">
<span>What are YOU passionate about?</span>
<textarea name="fields[whatAreYouPassionateAbout]"
placeholder="What are YOU passionate about?">{{ application.whatAreYouPassionateAbout }}</textarea>
</div>
<div class="col-sm-12 mt48 input-with-label">
<span>What songs are included on the soundtrack to your life?</span>
<textarea name="fields[lifeSoundtrack]"
placeholder="One of the most important question on this application.">{{ application.lifeSoundtrack }}</textarea>
</div>
</div>
<hr class="mt16">
<div class="row mt16">
<div class="skills col-sm-12 col-md-7 col-lg-9">
<div class="skills__list twelve columns">
<h3 class="skills__list-title">Add some skills by clicking the options on the right!</h3>
{% for bloc in application.skills %}
<div class="skills input-with-label text-left">
{% if bloc.type == "programmingLanguage" %}
<p>+ Programming Language</p>
<input type="hidden" name="fields[skills][new{{ loop.index }}][type]" value="{{ bloc.type }}">
<span>Language Name:</span>
<input type="text" name="fields[skills][new{{ loop.index }}][fields][languageName]" value="{{ bloc.languageName }}" placeholder="Language Name">
<span>Years Of Proficiency:</span>
<input type="text" name="fields[skills][new{{ loop.index }}][fields][yearsOfProficiency]" value="{{ bloc.yearsOfProficiency }}" placeholder="Years Of Proficiency">
{% elseif bloc.type == "programmingFramework"%}
<p>+ Programming Framework</p>
<input type='hidden' name='fields[skills][new{{ loop.index }}][type]' value="{{ bloc.type }}">
<span>Framework Name:</span>
<input type="text" name="fields[skills][new{{ loop.index }}][fields][frameworkName]" value="{{ bloc.frameworkName }}" placeholder="Framework Name">
<span>Coolest Thing You Have Built:</span>
<textarea name="fields[skills][new{{ loop.index }}][fields][coolestThing]" placeholder="What is the coolest thing you have built with it?">{{ bloc.coolestThing }}</textarea>
{% elseif bloc.type == "knowledge"%}
<p>+ Knowledge</p>
<input type='hidden' name='fields[skills][new{{ loop.index }}][type]' value="{{ bloc.type }}">
<span>Programmers alone can't solve these great challenges we need people like you with that special know-how, maybe it's in environmental sciences, maybe it's in Politics? Whatever you know something extra about:</span>
<textarea name="fields[skills][new{{ loop.index }}][fields][knowledge]" placeholder="Programmers alone can't solve these great challenges we need people like you with that special know-how, maybe it's in environmental sciences, maybe it's in Politics? Whatever you know something extra about!">{{ bloc.knowledge }}</textarea>
{% elseif bloc.type == "expirience"%}
<p>+ Expirience</p>
<input type='hidden' name='fields[skills][new{{ loop.index }}][type]' value="{{ bloc.type }}">
<span>Worked at one of those cool tech companies, or maybe been involved in hackathons in past?</span>
<textarea name="fields[skills][new{{ loop.index }}][fields][expirience]" placeholder="Worked at one of those cool tech companies, or maybe been involved in hackathons in past?">{{ bloc.knowledge }}</textarea>
{% endif %}
</div>
<hr>
{% endfor %}
</div>
</div>
<div class="col-sm-12 col-md-5 col-lg-3">
<div class="skills-buttons">
<a href="#" class="btn btn-sm skills__button" data-type="programmingLanguage">+ Programming Language</a>
<a href="#" class="btn btn-sm skills__button" data-type="programmingFramework">+ Programming Framework</a>
<a href="#" class="btn btn-sm skills__button" data-type="knowledge">+ Knowledge</a>
<a href="#" class="btn btn-sm skills__button" data-type="experience">+ Experience</a>
<!-- <a href="#" class="btn skills__button" data-type="proveYourAwesomness">+ Prove Your Awesomness</a>
<a href="#" class="btn skills__button" data-type="showYourThinkingProwess">+ Show Your Thinking Prowess</a> -->
</div>
</div>
</div>
<input type="submit" class="button button-primary" value="Continue">
</form>
</div>
</div>
</section>
{% includejs %}
var $skills = $('.skills__button');
{% if cart.skills %}
var $iter = {{ cart.skills|length }};
{% else %}
var $iter = 0;
{% endif %}
$skills.on('click', function() {
var $type = $(this).attr("data-type");
var $humanType = $(this).text();
$iter++;
console.log('Adding Block');
if($type == "programmingLanguage") {
$('.skills__list').append("\
<div class='skills'> \
<p>"+$humanType+"</p> \
<input type='hidden' name='fields[skills][new"+$iter+"][type]' value='"+$type+"'> \
<input type='text' name='fields[skills][new"+$iter+"][fields][languageName]' placeholder='Language Name'> \
<input type='text' name='fields[skills][new"+$iter+"][fields][yearsOfProficiency]' placeholder='Years Of Proficiency'> \
</div>");
}else if($type == "programmingFramework") {
$('.skills__list').append("\
<div class='skills'> \
<p>"+$humanType+"</p> \
<input type='hidden' name='fields[skills][new"+$iter+"][type]' value='"+$type+"'> \
<input type='text' name='fields[skills][new"+$iter+"][fields][frameworkName]' placeholder='Framework Name'> \
<textarea name='fields[skills][new"+$iter+"][fields][coolestThing]' placeholder='What is the coolest thing you have built with it?'></textarea> \
</div>");
}else if($type == "knowledge") {
$('.skills__list').append("\
<div class='skills'> \
<p>"+$humanType+"</p> \
<input type='hidden' name='fields[skills][new"+$iter+"][type]' value='"+$type+"'> \
<textarea name='fields[skills][new"+$iter+"][fields][knowledge]' placeholder='Programmers alone can't solve these great challenges we need people like you with that special know-how, maybe it's in environmental sciences, maybe it's in Politics? Whatever you know something extra about!'></textarea> \
</div>");
}else if($type == "expirience") {
$('.skills__list').append("\
<div class='skills'> \
<p>"+$humanType+"</p> \
<input type='hidden' name='fields[skills][new"+$iter+"][type]' value='"+$type+"'> \
<textarea name='fields[skills][new"+$iter+"][fields][expirience]' placeholder='Worked at one of those cool tech companies, or maybe been involved in hackathons in past?'></textarea> \
</div>");
}
});
{% endincludejs %}
{% endblock %}