Skip to content

Commit

Permalink
Removed some JS and added a check for autocomplete on form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmking committed Nov 3, 2020
1 parent 91283f0 commit e99f62d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,6 @@
</div>
</div>

<script>
window.onload = function () {
var ukprn = jQuery('#Ukprn');
var gfg = ukprn.closest('.govuk-form-group');
if (gfg.hasClass('govuk-form-group--error')) {
ukprn.css('border', '4px solid #df3034');
}
var ukprnForm = ukprn.closest('form');
jQuery('#Ukprn-button').on('click', function (e) {
e.preventDefault();
var ukprnVal = ukprn.val();
if (ukprnVal) {//has a value
ukprnForm.append('<input type="hidden" id="Ukprn-hidden" name="Ukprn-hidden" value="' + ukprnVal + '">').submit();
} else {
ukprnForm.submit();
}
});
ukprn.attr('type', 'search');
document.getElementById('Ukprn').addEventListener('search', function () {
ukprn.keyup(function () {
if (this.value.length === 3) {
document.getElementById('Ukprn__listbox').scrollTop = 0;
}
});
});//chrome supported to resolve chrome behaviour (resets list if cleared)
};
</script>

@section Back
{
<div class="das-js-back-link"></div>
Expand Down
24 changes: 20 additions & 4 deletions src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@
<script>
var idSelectField = 'Ukprn';
var selectEl = document.querySelector('#' + idSelectField);
var selectEl = document.getElementById(idSelectField);
if (selectEl) {
var form = $('#' + idSelectField).closest('form');
accessibleAutocomplete.enhanceSelectElement({
selectElement: selectEl,
minLength: 3,
Expand All @@ -118,11 +120,25 @@
this.selectElement.selectedIndex = 0;
}
}
});
$('#' + idSelectField).attr('type', 'search');
form.on('submit',
function() {
$('.autocomplete__input').each(function() {
var that = $(this);
if (that.val().length === 0) {
var fieldId = that.attr('id'),
selectField = $('#' + fieldId + '-select');
selectField[0].selectedIndex = 0;
}
});
});
}
</script>

@RenderSection("Scripts", required: false)

</body>
Expand Down

0 comments on commit e99f62d

Please sign in to comment.