How to show/hide elements based on multiple input groups with jQuery? - html

I have multiple product forms on one page. Within each form are multiple color options (radio buttons) with either an available or unavailable attribute.
I would like to show a custom add to cart button for each product depending on if the selected color option is available, ie. if input[available]:checked show .button-available else show .button-unavailable
Expected results
When the page loads, the first color option is always pre-selected. Depending on its availability, the corresponding button should be shown. When the selection changes, the button should change if necessary.
HTML structure
<div class="list-item">
<form action="/cart/add" id="add-to-cart-1">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="1" class="red" value="1" available>
<input type="radio" name="color" id="2" class="blue" value="2" unavailable>
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
<div class="list-item">
<form action="/cart/add" id="add-to-cart-2">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="3" class="red" value="3" unavailable>
<input type="radio" name="color" id="4" class="blue" value="4" available>
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
My sad attempt at a script
$(document).ready(function() {
var $list = $('.list-item');
$list.each(function(){
if ( $('input[name=color][unavailable]:checked').val() !== 'true' ) {
$(this).closest('.button-available').show();
}
if ( $('input[name=color][available]:checked').val() !== 'true' ) {
$(this).closest('.button-unavailable').show();
}
$('input[name=color]:checked').on('change', function() {
if ($(this).attr('available') == 'true' ) {
$(this).closest('.button-available').show();
$(this).closest('.button-unavailable').hide();
}
else {
$(this).attr('unavailable') == 'true' ) {
$(this).closest('.button-available').show();
$(this).closest('.button-unavailable').show();
}
});
});
Thank you in advance for your guidance.

I propose to you to change your attribute available/unavailable by using data().
closest give you the more clause element with class. You also need nextAll or prevAll() to get next ou previous element.
I may forgot to fix something, tell me if something is missing.
var $list = $('.list-item');
$list.each(function(){
if ( $(this).find('input[name=color][data-statut="unavailable"]').is(':checked') === false ) {
$(this).find('.button-available').show();
}
if ( $(this).find('input[name=color][data-statut="available"]').is(':checked') === false ) {
$(this).find('.button-unavailable').show();
}
});
$('input[name=color]').on('change', function() {
if ($(this).data('statut') == 'available' ) {
$(this).closest('.radios').nextAll('.button-available').show();
$(this).closest('.radios').nextAll('.button-unavailable').hide();
}
if ($(this).data('statut') == 'unavailable' ) {
$(this).closest('.radios').nextAll('.button-available').hide();
$(this).closest('.radios').nextAll('.button-unavailable').show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="list-item">
<form action="/cart/add" id="add-to-cart-1">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="1" class="red" value="1" data-statut="available">
<input type="radio" name="color" id="2" class="blue" value="2" data-statut="unavailable">
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
<div class="list-item">
<form action="/cart/add" id="add-to-cart-2">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="3" class="red" value="3" data-statut="available">
<input type="radio" name="color" id="4" class="blue" value="4" data-statut="unavailable">
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>

Related

jQuery form validation with checkbox / radio

The code is not working well at all. Where is the problem?
I want the Required Fields to show an error message when I click the Submit button (if there is no value in the checkbox). But if there is a value in the requested field, the error message will be hidden.
It's not working at all. I want to fix this by keeping my HTML structure straight.
$('form.trexcterc').on('submit', function () {
$('.checkbox_required').parent().next('.error').remove();
if (!$('.checkbox_required input[required')){
if (!$('.checkbox_required input:checkbox').is(':checked') || !$('.checkbox_required input:radio').is(':checked')) {
$(".checkbox_required").parent().after("<span class='error'>error message..</span>");
}
}
});
.trexcterc {
width: 100%;
margin: 0 auto;
padding: 0 30px;
}
.form_item label {
width: 100%;
}
.checkbox_heading {
margin: 0;
}
.checkbox_item{
display: flex;
margin: 15px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="trexcterc">
<div class="form_item" id="form_item_checkbox" data-type="checkbox-group">
<!-- Checkbox Group 1 -->
<label for="after_shot" class="checkbox_heading">After Shot<span class="required_sign">*</span></label>
<div class="checkbox-group checkbox-inline checkbox_required ">
<div class="checkbox_item">
<input type="checkbox" name="checkbox-one" id="checkbox-one" class="" value="Item One" required="required" />
<label for="checkbox-one"><span class="check_mark"></span>Item One</label>
</div>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-two" id="checkbox-two" class="" value="Item Two" required="required" />
<label for="checkbox-two"><span class="check_mark"></span>Item One</label>
</div>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-three" id="checkbox-three" class="" value="Item Three" required="required" />
<label for="checkbox-three"><span class="check_mark"></span>Item One</label>
</div>
</div>
<!-- Checkbox Group 2 -->
<div class="checkbox-group checkbox-inline checkbox_required ">
<label for="after_shot" class="checkbox_heading">Before Shot<span class="required_sign">*</span></label>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-item-one" id="checkbox-item-one" class="" value="Item One" required="required" />
<label for="checkbox-one"><span class="check_mark"></span>Item One</label>
</div>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-two" id="checkbox-two" class="" value="Item Two" required="required" />
<label for="checkbox-two"><span class="check_mark"></span>Item One</label>
</div>
</div>
</div>
<input type="submit" name="submit" value="submit" />
</form>
Main issue is that the native required attribute is preventing form submission, so your submit event listener isn't triggered. Instead you can use a simple click event listener.
Another issue is that you need to iterate over each input.
$('button').on('click', function() {
$('.error').remove();
$('.checkbox_required input[required]').each((i, el) => {
if (!el.checked) {
$(el).closest(".checkbox_item").after("<span class='error'>error message..</span>");
}
});
});
.trexcterc {
width: 100%;
margin: 0 auto;
padding: 0 30px;
}
.form_item label {
width: 100%;
}
.checkbox_heading {
margin: 0;
}
.checkbox_item {
display: flex;
margin: 15px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="trexcterc">
<div class="form_item" id="form_item_checkbox" data-type="checkbox-group">
<!-- Checkbox Group 1 -->
<label for="after_shot" class="checkbox_heading">After Shot<span class="required_sign">*</span></label>
<div class="checkbox-group checkbox-inline checkbox_required ">
<div class="checkbox_item">
<input type="checkbox" name="checkbox-one" id="checkbox-one" class="" value="Item One" required="required" />
<label for="checkbox-one"><span class="check_mark"></span>Item One</label>
</div>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-two" id="checkbox-two" class="" value="Item Two" required="required" />
<label for="checkbox-two"><span class="check_mark"></span>Item One</label>
</div>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-three" id="checkbox-three" class="" value="Item Three" required="required" />
<label for="checkbox-three"><span class="check_mark"></span>Item One</label>
</div>
</div>
<!-- Checkbox Group 2 -->
<div class="checkbox-group checkbox-inline checkbox_required ">
<label for="after_shot" class="checkbox_heading">Before Shot<span class="required_sign">*</span></label>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-item-one" id="checkbox-item-one" class="" value="Item One" required="required" />
<label for="checkbox-one"><span class="check_mark"></span>Item One</label>
</div>
<div class="checkbox_item">
<input type="checkbox" name="checkbox-two" id="checkbox-two" class="" value="Item Two" required="required" />
<label for="checkbox-two"><span class="check_mark"></span>Item One</label>
</div>
</div>
</div>
<button type="submit" name="submit">submit</button>
</form>
Also note that conditions like this: if (!$('.checkbox_required input[required')) doesn't work in jQuery, since selectors always returns a jQuery object. You need to use .length to check if element exists. You also have a missing ] there, it should be
if (!$('.checkbox_required input[required]').length)

How to validate a text input based on radio selected and if visible

I have a set of radio. If I select Value=1 the show custom_281 text field and if it is visible then should have some value before proceeding, if empty show an alert. If selected value=2 then hide custom_281 and not mandatory to fill in.
I cant seem to validate the text field and show the alert.
Here’s my html And jquery. Any help would be appreciated. Thanks
<div id="editrow-custom_280" class="crm-section editrow_custom_280-section form-item">
<div class="content">
<input type="radio" id="QFID_1_custom_280" name="custom_280" class="required crm-form-radio" value="1">
<input type="radio" id="QFID_2_custom_280" name="custom_280" class="required crm-form-radio" value="2">
</div>
</div>
<div id="editrow-custom_281" class="crm-section editrow_custom_281-section form-item" novalidate="novalidate" style="display: block;">
<div class="content">
<input id="custom_281" type="text" name="custom_281" maxlength="255" class="crm-form-text" placeholder="Enter Number ">
</div>
</div>
CRM.$(function($) {
showNumber();
function showNumber() {
$('input[name=custom_280]').change(showNumber);
if ($('input[name=custom_280][value=1]').is(':checked')) {
$('div#editrow-custom_281').slideDown('slow').validate({ignore:':not(:visible)'});
}
else {
$('div#editrow-custom_281').slideUp('slow');
}
}
});
You can check if the radio button checked value is 1 if yes check if the input is not empty depending on this show you message.
Demo Code :
showNumber();
function showNumber() {
$('input[name=custom_280]').change(showNumber);
if ($('input[name=custom_280][value=1]').is(':checked')) {
$('div#editrow-custom_281 input').val('')
$('div#editrow-custom_281').slideDown('slow')
} else {
$('div#editrow-custom_281').slideUp('slow');
}
}
//on click of proceed btn
$("#proceed").on("click", function() {
//check if the checked valeu is 1
if ($('input[name=custom_280]:checked').val() === '1') {
if (!$('div#editrow-custom_281 input').val()) {
$('div#editrow-custom_281 input').focus()
console.log("Please fill") //empty
} else {
console.log("All good")
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="crm-section editrow_custom_280-section form-item" id="editrow-custom_280">
<div class="content">
<input class=" required crm-form-radio" value="1" type="radio" id="QFID_1_custom_280" name="custom_280">
<input class=" required crm-form-radio" value="2" type="radio" id="QFID_2_custom_280" name="custom_280">
</div>
</div>
<div class="crm-section editrow_custom_281-section form-item" id="editrow-custom_281" novalidate="novalidate" style="display: block;">
<div class="content">
<input maxlength="255" name="custom_281" type="text" id="custom_281" class="crm-form-text" placeholder=" Enter Number ">
</div>
</div>
<button id="proceed">Proceed</button>

Allowing one radio button checked when radio inputs have different names

I'm trying to figure out how to only allow one of two radio buttons to be checked in a form where the two radio buttons don't share the same name. I've been experimenting for hours now, but can't figure it out. Here is what I have currently:
$("#theForm").click(function(){
//alert('You clicked radio!');
if($('input:radio:checked')){
$('input:radio:checked').prop("checked", false);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="theForm">
<form class="type">
<input type="radio" id="topic" name="topic" value="on" checked>
<input type="radio" id="all" name="all" value="on">
</form>
</div>
Here's a Fiddle: https://jsfiddle.net/Codewalker/reh0mzs3/1/
the click function will give you an event. you can target the event.target.name to see which radio button was clicked on and then do a simple if statement to uncheck the other radio button
$("#theForm").click(function(){
if(event.target.name == 'topic'){
$('#all').prop("checked", false);
}else{
$('#topic').prop("checked", false);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="theForm">
<form class="type">
<input type="radio" id="topic" name="topic" value="on" checked>
<input type="radio" id="all" name="all" value="on">
</form>
</div>
This should work:
<div> <input type="radio" id="contactChoice1" name="contact" value="email">
<label for="contactChoice1">Email</label>
<input type="radio" id="contactChoice2" name="contact" value="phone">
<label for="contactChoice2">Phone</label>
<input type="radio" id="contactChoice3" name="contact" value="message">
<label for="contactChoice3">Message</label> </div>

One label for multiuple inputs (radio)

$('label').click(function() {
id = this.id.split('-');
if (id[0] === '1') {
id[0] = '2';
} else {
id[0] = '1';
}
$('#' + id[0] + '-' + id[1]).prop('checked', true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="one">
<input type="radio" id="1-1" name="1-level">
<label for="1-1" id="1-1">1</label>
<input type="radio" id="1-2" name="1-level">
<label for="1-2" id="1-2">2</label>
</div>
<div class="two">
<input type="radio" id="2-1" name="2-level">
<label for="2-1" id="2-1">1</label>
<input type="radio" id="2-2" name="2-level">
<label for="2-2" id="2-2">2</label>
</div>
Is it possible to have one label for multiple inputs (radio)? So if I press the label (e. q. for="1"), two input fields get checked (input with id 1 in div class one and two)? Here is my example:
<div class="one">
<input type="radio" id="1" name="level">
<label for="1">1</label>
<input type="radio" id="2" name="level">
<label for="2">2</label>
</div>
<div class="two">
<input type="radio" id="1" name="level">
<label for="1">1</label>
<input type="radio" id="2" name="level">
<label for="2">2</label>
</div>
Quote from ducumentation:
https://www.w3.org/TR/html401/interact/forms.html#h-17.9.1
The LABEL element may be used to attach information to controls. Each
LABEL element is associated with exactly one form control.
So only way to do what you asked is by using JS
HTML:
Use data-input-group-id instead of id and data-for-input-group instead of for. Also you'll now be able to use "1" and "2" not "1-1" etc.
JS:
$('[data-for-input-group]').click(function() {
let inputGroupId = this.dataset.inputGroupId;
$('[data-input-group-id = "'+inputGroupId+'"]').prop('checked', true);
});

jquery mobile - dynamically add radio button

for this jsfiddle - http://jsfiddle.net/Ja3Fx/1/ $('div').trigger('create') doesn't
work as required.
it does style radio button but it isn't quite right ... any ideas ?
<div id="testPage" data-role="page">
<div data-role="content">
<fieldset data-role="controlgroup">
<legend>Radio buttons, vertical controlgroup:</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked">
<label for="radio-choice-1">Cat</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2">
<label for="radio-choice-2">Dog</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3">
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4">
<label for="radio-choice-4">Lizard</label>
</fieldset>
<a id="add-animal" href="#" data-role="button" data-position-to="window">Add Animal</a>
</div>
</div>
$('#add-animal').click(function() {
var fldset = $('fieldset');
$(fldset).append($('<input type="radio" name="radio-choice-1" id="radio-choice-5" value="choice-5"><label for="radio-choice-5">Bat</label>'));
$("input").checkboxradio();
$('div ').trigger('create ');
});
I am using the following code to make a panel. You can try similar code.
$(document).on('pagecreate', '[data-role="page"]', function(){
$('<div>').attr({'id':'mypanel','data-role':'panel'}).appendTo($(this));
$(document).on('click', '#open-panel', function(){
$.mobile.activePage.find('#mypanel').panel("open");
});
});