Reputation: 811
I'm creating a multi-step popup form and only want the #next
button to be active if the input fields are selected. I've managed to get it working for checkboxes and radio fields but for some reason it's not working for text and textarea fields.
I'm using the same code as the checkbox and radio fields but instead of checking is(:checked)
I'm targeting .val() != ""
.
When testing this, It only works if I go to the .multi-step-section
, fill in the text field, go back to the previous screen and then back to the section with the field. I think the active class is still looking at the previous section.
Is there a way of checking to make sure the input[type="text"]
and input[type="textarea"]
fields are checked on the current active screen before disabling the button?
Link to full popup form: https://jsfiddle.net/5yd23jah/
$(function() {
$('.multi-form-actions button').on('click', function() {
var el = $(this).attr('id');
var active = $('.multi-step-section.active');
active.add(active.siblings()).removeClass('active');
if (el == 'prev') {
active = active.prev('.multi-step-section').addClass('active');
$('#progressbar > span').css({
'width': $('#progressbar > span').width() / $('#progressbar').width() * 100 - sectionProgress + '%'
});
} else if (el = 'next') {
active = active.next('.multi-step-section').addClass('active');
$('#progressbar > span').css({
'width': $('#progressbar > span').width() / $('#progressbar').width() * 100 + sectionProgress + '%'
});
}
if ((active).find("input[type='radio'], input[type='checkbox']").is(':checked')) {
$('#next').removeAttr('disabled');
} else if ((active).find("input[type='text'], input[type='textarea']").val() != "") {
console.log('not empty');
$('#next').removeAttr('disabled');
} else {
$('#next').attr('disabled', 'disabled');
}
if (active.is(":not(:first-child)")) {
$('#prev').show();
} else {
$('#prev').hide();
}
if (active.is(":last-child")) {
$('#next').hide();
$('#submit').show();
} else {
$('#next').show();
$('#submit').hide();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="multi-step-wrapper" id="multi-step">
<div id="progressbar">
<span></span>
</div>
<div class="steps-inner">
<div class="multi-step-section active" id="step1">
<h3>Question 1</h3>
<p><span class="wpcf7-form-control-wrap radio-185"><span class="wpcf7-form-control wpcf7-checkbox"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">Loft</span><input name="radio-185[]" type="checkbox" value="Loft"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Garage ceiling</span><input name="radio-185[]" type="checkbox" value="Garage ceiling"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Floor</span><input name="radio-185[]" type="checkbox" value="Floor"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Ceilings</span><input name="radio-185[]" type="checkbox" value="Ceilings"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Stud walls</span><input name="radio-185[]" type="checkbox" value="Stud walls"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">Special project</span><input name="radio-185[]" type="checkbox" value="Special project"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step2">
<h3>Question 2</h3>
<p><span class="wpcf7-form-control-wrap text-465"><input aria-invalid="false" class="wpcf7-form-control wpcf7-text" name="text-465" placeholder="Enter postcode, city or address" size="40" type="text" value=""></span></p>
</div>
<div class="multi-step-section" id="step3">
<h3>Question 3</h3>
<p><span class="wpcf7-form-control-wrap radio-187"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">up to 30m2</span><input name="radio-187" type="radio" value="up to 30m2"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">30-60m2</span><input name="radio-187" type="radio" value="30-60m2"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">60-100m2</span><input name="radio-187" type="radio" value="60-100m2"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">0ver 100m2</span><input name="radio-187" type="radio" value="0ver 100m2"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step4">
<h3>Question 4</h3>
<p><span class="wpcf7-form-control-wrap radio-188"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">Ready to go</span><input name="radio-188" type="radio" value="Ready to go"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">In the next couple of months</span><input name="radio-188" type="radio" value="In the next couple of months"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">In a few months</span><input name="radio-188" type="radio" value="In a few months"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step5">
<h3>Question 5</h3>
<p><span class="wpcf7-form-control-wrap radio-189"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">Domestic</span><input name="radio-189" type="radio" value="Domestic"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Commercial</span><input name="radio-189" type="radio" value="Commercial"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">Construction</span><input name="radio-189" type="radio" value="Construction"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step6">
<h3>Question 6</h3>
<p><span class="wpcf7-form-control-wrap email-871"><input aria-invalid="false" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-email" name="email-871" placeholder="Enter your email address" size="40" type="email" value=""></span></p>
<div>
or
</div>
<p><span class="wpcf7-form-control-wrap tel-525"><input aria-invalid="false" aria-required="true" class="wpcf7-form-control wpcf7-text wpcf7-tel wpcf7-validates-as-required wpcf7-validates-as-tel" name="tel-525" placeholder="Enter your telephone number" size="40" type="tel" value=""></span></p>
</div>
</div>
<div class="multi-form-actions">
<button id="prev" type="button">Back</button><button disabled id="next" type="button">Continue</button><input class="wpcf7-form-control wpcf7-submit multiform-submit" id="submit" type="submit" value="Send"><br>
<input class="sr-only" id="reset" type="reset">
</div>
</div>
Upvotes: 0
Views: 127
Reputation: 177786
You mean
var active = $('.multi-step-section.active');
var dis = active.find("input[type='radio']:checked").length == 0;
dis = dis && active.find("input[type='checkbox']:checked").length == 0;
dis = dis && active
.find("input:text,textarea")
.map(function(idx, elem) {
return $(elem).val() || null; // beware of valid values of 0 (zero)
}).get().length == 0;
$('#next').prop('disabled', dis);
Code for progressbar:
$('#pBar').prop("max",$('.multi-step-section').length).val(0); // loading
...
$('#pBar').val(active.index()); // each click on the prev/next
Running example
// reusable function
function checkInput() {
var active = $('.multi-step-section.active');
var dis = active.find("input[type='radio']:checked").length == 0;
dis = dis && active.find("input[type='checkbox']:checked").length == 0;
dis = dis && active
.find("input:text,textarea")
.map(function(idx, elem) {
return $(elem).val() || null;
}).get().length == 0;
$('#next').prop('disabled', dis);
}
$(function() {
$(":input").on("input", checkInput); // any input
$('#pBar').prop("max",$('.multi-step-section').length).val(0);
$('.multi-form-actions button').on('click', function() {
var el = $(this).attr('id');
var active = $('.multi-step-section.active');
var count = $('.multi-step-section').length;
active.add(active.siblings()).removeClass('active');
if (el == 'prev') {
active = active.prev('.multi-step-section').addClass('active');
} else if (el = 'next') {
active = active.next('.multi-step-section').addClass('active');
}
$('#pBar').val(active.index());
checkInput(); // reset the continue if needed
if (active.is(":not(:first-child)")) {
$('#prev').show();
} else {
$('#prev').hide();
}
if (active.is(":last-child")) {
$('#next').hide();
$('#submit').show();
} else {
$('#next').show();
$('#submit').hide();
}
});
});
.multi-step-section {
display: none
}
.active {
display: block
}
#progressbar { background-color:green;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="multi-step-wrapper" id="multi-step">
<div id="progressbar">
<span></span>
</div>
<div class="steps-inner">
<div class="multi-step-section active" id="step1">
<h3>Question 1</h3>
<p><span class="wpcf7-form-control-wrap radio-185"><span class="wpcf7-form-control wpcf7-checkbox"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">Loft</span><input name="radio-185[]" type="checkbox" value="Loft"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Garage ceiling</span><input name="radio-185[]" type="checkbox" value="Garage ceiling"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Floor</span><input name="radio-185[]" type="checkbox" value="Floor"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Ceilings</span><input name="radio-185[]" type="checkbox" value="Ceilings"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Stud walls</span><input name="radio-185[]" type="checkbox" value="Stud walls"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">Special project</span><input name="radio-185[]" type="checkbox" value="Special project"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step2">
<h3>Question 2</h3>
<p><span class="wpcf7-form-control-wrap text-465"><input aria-invalid="false" class="wpcf7-form-control wpcf7-text" name="text-465" placeholder="Enter postcode, city or address" size="40" type="text" value=""></span></p>
</div>
<div class="multi-step-section" id="step3">
<h3>Question 3</h3>
<p><span class="wpcf7-form-control-wrap radio-187"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">up to 30m2</span><input name="radio-187" type="radio" value="up to 30m2"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">30-60m2</span><input name="radio-187" type="radio" value="30-60m2"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">60-100m2</span><input name="radio-187" type="radio" value="60-100m2"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">0ver 100m2</span><input name="radio-187" type="radio" value="0ver 100m2"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step4">
<h3>Question 4</h3>
<p><span class="wpcf7-form-control-wrap radio-188"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">Ready to go</span><input name="radio-188" type="radio" value="Ready to go"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">In the next couple of months</span><input name="radio-188" type="radio" value="In the next couple of months"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">In a few months</span><input name="radio-188" type="radio" value="In a few months"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step5">
<h3>Question 5</h3>
<p><span class="wpcf7-form-control-wrap radio-189"><span class="wpcf7-form-control wpcf7-radio"><span class="wpcf7-list-item first"><label><span class="wpcf7-list-item-label">Domestic</span><input name="radio-189" type="radio" value="Domestic"></label>
</span><span class="wpcf7-list-item"><label><span class="wpcf7-list-item-label">Commercial</span><input name="radio-189" type="radio" value="Commercial"></label>
</span><span class="wpcf7-list-item last"><label><span class="wpcf7-list-item-label">Construction</span><input name="radio-189" type="radio" value="Construction"></label>
</span>
</span>
</span>
</p>
</div>
<div class="multi-step-section" id="step6">
<h3>Question 6</h3>
<p><span class="wpcf7-form-control-wrap email-871"><input aria-invalid="false" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-email" name="email-871" placeholder="Enter your email address" size="40" type="email" value=""></span></p>
<div>
or
</div>
<p><span class="wpcf7-form-control-wrap tel-525"><input aria-invalid="false" aria-required="true" class="wpcf7-form-control wpcf7-text wpcf7-tel wpcf7-validates-as-required wpcf7-validates-as-tel" name="tel-525" placeholder="Enter your telephone number" size="40" type="tel" value=""></span></p>
</div>
</div>
<div class="multi-form-actions">
<button id="prev" type="button">Back</button><button disabled id="next" type="button">Continue</button><input class="wpcf7-form-control wpcf7-submit multiform-submit" id="submit" type="submit" value="Send"><br>
<input class="sr-only" id="reset" type="reset">
</div>
</div>
<progress id="pBar" value="0">70 %</progress>
Upvotes: 2