{% extends "base.html" %}

{% block title %}Bulk add candidates{% endblock %}

{% block content %}
<h2>Add candidates to {{ election }}: {{ post_id }}</h2>
<form method=POST>
  {% csrf_token %}
  {{ formset.management_form }}
  {% for form in formset %}
    <h3>Person {{ forloop.counter }}</h3>
    {{ form.as_p }}
  {% endfor %}
  <button type=submit>Next</button>
</form>

<script>
// Thanks to http://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js
var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

var source = getUrlParameter('source');

$('[name=form-0-source]').val(source);



</script>
{% endblock content %}

{% extends "base.html" %}

{% block title %}Bulk add candidates{% endblock %}

{% block content %}
<h2>Add candidates to {{ election }}: {{ post_id }}</h2>
<form method=POST id="bulk_add_form">
  {% csrf_token %}
  {{ formset.management_form }}
  {% for form in formset %}
    <div class="form_group">
    <h3>Person {{ forloop.counter }}</h3>
    {{ form.as_p }}
    </div>
  {% endfor %}
  <button type=submit>Next</button>
</form>

<script>
// Thanks to http://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js
var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

var source = getUrlParameter('source');

$('[name*=-source]').val(source);

$("#bulk_add_form").submit(function(e) {
  $('[name*=-source]').each(function(e, source_el) {

    var source_el = $(source_el);
    var name_el = $($(source_el).parent().parent().find('input[id*=name]'));

    if (name_el.val() == "") {
      source_el.val("");
    }

  });
})


</script>
{% endblock content %}

Clear source inputs if name is empty
Clear source inputs if name is empty
