Selecting the closest input to adjust value - javascript

I have 3 input boxes, with radio buttons above each input box that fill in the values of the boxes. 5 Radio buttons for each box. Each box/button set should act independantly. They are created with the same classes and I adjust the value in the input boxes based on the radio button with the onclick radio button listener like so
document.getElementsByClassName('nyp-input')[0].value=this.value;
document.getElementsByClassName('nyp-input')[1].value=this.value;
document.getElementsByClassName('nyp-input')[2].value=this.value;
Which doesn't work because it adjusts all 3, but I need it to adjust only the closest input. I've tried the below
jQuery('input').closest('nyp').find('.nyp-input');
adj.value=this.value;
The full snippet in context
<div class="nyp" data-price="0" data-max-price="" >
<div class="price-wrapper">
<div class="donation-options">
<label for="nyp-suggested-1">
<input type="radio" name="nyp-suggested" value="20" id="nyp-suggested-1" onclick="
var adj = jQuery('input').closest('nyp').find('.nyp-input');
adj.value=this.value;
"> $20
</label>
<label for="nyp-suggested-2">
<input type="radio" name="nyp-suggested" value="50" id="nyp-suggested-2" onclick="document.getElementsByClassName('nyp-input')[0].value=this.value; document.getElementsByClassName('nyp-input')[1].value=this.value; document.getElementsByClassName('nyp-input')[2].value=this.value;"> $50
</label>
<label for="nyp-suggested-3">
<input type="radio" name="nyp-suggested" value="100" id="nyp-suggested-3" onclick="document.getElementsByClassName('nyp-input')[0].value=this.value; document.getElementsByClassName('nyp-input')[1].value=this.value; document.getElementsByClassName('nyp-input')[2].value=this.value;"> $100
</label>
<label for="nyp-suggested-4">
<input type="radio" name="nyp-suggested" value="500" id="nyp-suggested-4" onclick="document.getElementsByClassName('nyp-input')[0].value=this.value; document.getElementsByClassName('nyp-input')[1].value=this.value; document.getElementsByClassName('nyp-input')[2].value=this.value;"> $500
</label>
</div>
</div>
<label for="nyp">
Other ( $ ) </label>
<input id="nyp" name="nyp" type="text" value="0.00" title="nyp" class="input-text amount nyp-input text">
How can I get the closest nyp-input to adjust the value.

try this code
$(function(){
$('.nyp-radio').click(function(){
$(this).parents('.price-wrapper').children('[type="text"]').val($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nyp" data-price="0" data-max-price="" >
<div class="price-wrapper">
<div class="donation-options">
<label for="nyp-suggested-1">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="20" id="nyp-suggested-1" > $20
</label>
<label for="nyp-suggested-2">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="50" id="nyp-suggested-2" > $50
</label>
<label for="nyp-suggested-3">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="100" id="nyp-suggested-3" > $100
</label>
<label for="nyp-suggested-4">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="500" id="nyp-suggested-4" > $500
</label>
</div>
<label for="nyp">
Other ( $ ) </label>
<input id="nyp" name="nyp" type="text" value="0.00" title="nyp" class="input-text amount nyp-input text">
</div>
<div class="nyp" data-price="0" data-max-price="" >
<div class="price-wrapper">
<div class="donation-options">
<label for="nyp-suggested-11">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="20" id="nyp-suggested-11" > $20
</label>
<label for="nyp-suggested-12">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="50" id="nyp-suggested-12" > $50
</label>
<label for="nyp-suggested-13">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="100" id="nyp-suggested-13" > $100
</label>
<label for="nyp-suggested-14">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="500" id="nyp-suggested-14" > $500
</label>
</div>
<label for="nyp">
Other ( $ ) </label>
<input id="nyp" name="nyp" type="text" value="0.00" title="nyp" class="input-text amount nyp-input text">
</div>

The problem you may have is that the 3 "input boxes" contain inputs with the same IDs (basically you have for example nyp-suggested-1 3 times). You should try and generate different IDs keeping the same name for each group, but distinct between groups -example:
group1:
<label for="nya1">
<input type="radio" name="nya" value="20" id="nya1" > $20
</label>
<label for="nya2">
<input type="radio" name="nya" value="50" id="nya2" > $50
</label>
<label for="nya3">
<input type="radio" name="nya" value="100" id="nya3" > $100
</label>
<label for="nya4">
<input type="radio" name="nya" value="500" id="nya4" > $500
</label>
group 2:
<label for="nyb1">
<input type="radio" name="nyb" value="20" id="nyb1" > $20
</label>
<label for="nyb2">
<input type="radio" name="nyb" value="50" id="nyb2" > $50
</label>
<label for="nyb3">
<input type="radio" name="nyb" value="100" id="nyb3" > $100
</label>
<label for="nyb4">
<input type="radio" name="nyb" value="500" id="nyb4" > $500
</label>
Then you can select based on group name in jquery and update using the same value.

Related

How to auto check radio button basis of two radio button groups

<div class="product_colors">
<span class="header" style="display:block;">Color</span>
<label class="color_label active">
<input type="radio" name="product_color" value="gold" >Gold
<span style="""></span>
</label>
<label class="color_label">
<input type="radio" name="product_color" value="rose gold">Rose Gold
<span style="" data-title="rose gold"></span>
</label>
<label class="color_label">
<input type="radio" name="product_color" value="silver" > Silver
<span style="" data-title="silver"></span>
</label>
</div>
<div class="product_stones">
<span class="header" style="display:block;">Stone</span>
<label class="stone_label">
<input type="radio" name="product_stone" value="malachite" >malachite
</label>
<label class="stone_label active">
<input type="radio" name="product_stone" value="tiger-eye" > tiger-eye
</label>
<label class="stone_label">
<input type="radio" name="product_stone" value="black" > black
</label>
<label class="stone_label">
<input type="radio" name="product_stone" value="blue" > blue
</label>
</div>
<div class="combination">
Combination
<div class="hidden">
<input type="radio" name="combination" class="hidden_wrap" data-color="gold" data-stone="malachite" data-handle="joory-earring-malachite-gold"> gold-malachite
<input type="radio" name="combination" class="hidden_wrap" data-color="gold" data-stone="tiger-eye" data-handle="copy-of-joory-earrings-tiger-eye-gold">gold-tiger-eye
<input type="radio" name="combination" class="hidden_wrap" data-color="rose gold" data-stone="black" data-handle="kanz-ring-black-rose-gold"> rose-gold-black
<input type="radio" name="combination" class="hidden_wrap" data-color="silver" data-stone="blue" data-handle="joory-earring-blue-silver"> silver-blue
</div>
</div>
here is my output image
I want to change combination radio button value to be select automatically on the basis of selected color and stone. Can someone help me to get rid of this problem.
Here is my code I've tried everything but I am new in JS so I can't find solution of this problem
Color
Gold
Rose Gold
Silver
<div class="product_stones">
<span class="header" style="display:block;">Stone</span>
<label class="stone_label">
<input type="radio" name="product_stone" value="malachite" >malachite
</label>
<label class="stone_label active">
<input type="radio" name="product_stone" value="tiger-eye" > tiger-eye
</label>
<label class="stone_label">
<input type="radio" name="product_stone" value="black" > black
</label>
<label class="stone_label">
<input type="radio" name="product_stone" value="blue" > blue
</label>
</div>
<div class="combination">
Combination
<div class="hidden">
<input type="radio" name="combination" class="hidden_wrap" data-color="gold" data-stone="malachite" data-handle="joory-earring-malachite-gold"> gold-malachite
<input type="radio" name="combination" class="hidden_wrap" data-color="gold" data-stone="tiger-eye" data-handle="copy-of-joory-earrings-tiger-eye-gold">gold-tiger-eye
<input type="radio" name="combination" class="hidden_wrap" data-color="rose gold" data-stone="black" data-handle="kanz-ring-black-rose-gold"> rose-gold-black
<input type="radio" name="combination" class="hidden_wrap" data-color="silver" data-stone="blue" data-handle="joory-earring-blue-silver"> silver-blue
</div>
</div>
Here is another solution I've tried which is not edit HTML.
My solution is going to find combination radio button that has proper data-color and data-stone
$("input[name^='product_']").on('change', (e) => {
$("input[name='combination']").prop('checked', false);
var productColor = $("input[name='product_color']:checked").val();
var productStone = $("input[name='product_stone']:checked").val();
if (productColor !== undefined && productStone !== undefined) {
if ($(`input[data-color='${productColor}'][data-stone='${productStone}']`).length) {
$(`input[data-color='${productColor}'][data-stone='${productStone}']`).trigger('click');
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product_colors">
<span class="header" style="display:block;">Color</span>
<label class="color_label active">
<input type="radio" name="product_color" value="gold" >Gold
<span style="""></span>
</label>
<label class="color_label">
<input type="radio" name="product_color" value="rose gold">Rose Gold
<span style="" data-title="rose gold"></span>
</label>
<label class="color_label">
<input type="radio" name="product_color" value="silver" > Silver
<span style="" data-title="silver"></span>
</label>
</div>
<div class="product_stones">
<span class="header" style="display:block;">Stone</span>
<label class="stone_label">
<input type="radio" name="product_stone" value="malachite" >malachite
</label>
<label class="stone_label active">
<input type="radio" name="product_stone" value="tiger-eye" > tiger-eye
</label>
<label class="stone_label">
<input type="radio" name="product_stone" value="black" > black
</label>
<label class="stone_label">
<input type="radio" name="product_stone" value="blue" > blue
</label>
</div>
<div class="combination">
Combination
<div class="hidden">
<input type="radio" name="combination" class="hidden_wrap" data-color="gold" data-stone="malachite" data-handle="joory-earring-malachite-gold"> gold-malachite
<input type="radio" name="combination" class="hidden_wrap" data-color="gold" data-stone="tiger-eye" data-handle="copy-of-joory-earrings-tiger-eye-gold">gold-tiger-eye
<input type="radio" name="combination" class="hidden_wrap" data-color="rose gold" data-stone="black" data-handle="kanz-ring-black-rose-gold"> rose-gold-black
<input type="radio" name="combination" class="hidden_wrap" data-color="silver" data-stone="blue" data-handle="joory-earring-blue-silver"> silver-blue
</div>
</div>
You need some jQuery to achieve this
<script>
$(document).ready(function() {
$('.product_description').click(function() {
var color = $('input[name="product_color"]:checked').val();
var stone = $('input[name="product_stone"]:checked').val();
var combinationClass = color + "-" + stone
if ($('.' + combinationClass).length) {
$('.' + combinationClass).attr('checked', true)
} else {
//If the class name of the combination is not found
alert("something went wrong");
}
});
});
</script>
HTML Code
<!--
# Added common class for all the stone and colour radio buttons to enable trigger
# Added Class based on the colour and stone value in the combination
# Added checked attribute to stone and colour radio buttons for on Load default selection
-->
<div class="product_colors">
<span class="header" style="display:block;">Color</span>
<label class="color_label">
<input type="radio" class="product_description" name="product_color" value="gold" checked>Gold
<span></span>
</label>
<label class="color_label">
<input type="radio" class="product_description" name="product_color" value="rose-gold">Rose Gold
<span data-title="rose gold"></span>
</label>
<label class="color_label">
<input type="radio" class="product_description" name="product_color" value="silver"> Silver
<span data-title="silver"></span>
</label>
</div>
<div class="product_stones">
<span class="header" style="display:block;">Stone</span>
<label class="stone_label">
<input type="radio" class="product_description" name="product_stone" value="malachite" checked>malachite
</label>
<label class="stone_label active">
<input type="radio" class="product_description" name="product_stone" value="tiger-eye"> tiger-eye
</label>
<label class="stone_label">
<input type="radio" class="product_description" name="product_stone" value="black"> black
</label>
<label class="stone_label">
<input type="radio" class="product_description" name="product_stone" value="blue"> blue
</label>
</div>
<div class="combination">
Combination
<div class="hidden">
<input type="radio" name="combination" class="hidden_wrap gold-malachite" data-color="gold" data-stone="malachite" data-handle="joory-earring-malachite-gold"> gold-malachite
<input type="radio" name="combination" class="hidden_wrap gold-toger-eye" data-color="gold" data-stone="tiger-eye" data-handle="copy-of-joory-earrings-tiger-eye-gold">gold-tiger-eye
<input type="radio" name="combination" class="hidden_wrap rose-gold-black" data-color="rose gold" data-stone="black" data-handle="kanz-ring-black-rose-gold"> rose-gold-black
<input type="radio" name="combination" class="hidden_wrap silver-blue" data-color="silver" data-stone="blue" data-handle="joory-earring-blue-silver"> silver-blue
</div>
</div>

How to send values of selected check boxes as a concatenated string in a text input on-click to a action button

Let's assume I have an array of check boxes (not necessarily as a part of a form). In my case 2 rows with 16 check boxes per row.
Each has his unique ID, name and value.
I need An action button to send to a form a concatenated string of selected check boxes and also to another input the number of selected boxes (count of checked) This inputs are part of a form containing and other inputs.
This is my basic functionality diagram:
diagram of1
My question for you is how to achieve this in a stable and safe manner. JS, Jquery, other methods?
Thank you,
Here is my html/Jsquery work:
//Select interval with Shift
$(document).ready(function() {
var $chkboxes = $('.chkbox');
var lastChecked = null;
$chkboxes.click(function(e) {
if (!lastChecked) {
lastChecked = this;
return;
}
if (e.shiftKey) {
var start = $chkboxes.index(this);
var end = $chkboxes.index(lastChecked);
$chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1).prop('checked', lastChecked.checked);
}
lastChecked = this;
});
});
//count checked
var form = document.getElementById('chart');
var checkBoxes = $(form).children('.chkbox');
var count = 0;
var divBoxesChecked = document.getElementById('boxesChecked');
divBoxesChecked.innerHTML = 0;
$(checkBoxes).on('change', function() {
$.each(checkBoxes, function(i) {
if (checkBoxes[i].checked) {
count++;
}
});
var qty = count;
var seld = $('.chkbox:checked').map(function() {
return this.value;
})
.get()
.join(",");
$("#transfercape").on('click', function() {
var pid = 125;
var price = 4;
$('#productprice').val(price);
$('#productID').val(pid);
$('#elements').val(qty);
$('#selectionprice').val(price * qty);
$('#teeth').val(seld);
});
$("#transferbonturi").on('click', function() {
var pid = 155;
var price = 40;
$('#productprice').val(price);
$('#productID').val(pid);
$('#elements').val(qty);
$('#selectionprice').val(price * qty);
$('#teeth').val(seld);
});
console.log(count);
//console.log($('input[class=chkbox]:checked').val(","));
divBoxesChecked.innerHTML = count + " elemente selectate ";
console.log(seld);
count = 0;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chart">
<h1>Chart</h1>
<input type="checkbox" class="chkbox" id="maxila18" name="maxila18" value="18">
<label for="maxila1"> 18</label>
<input type="checkbox" class="chkbox" id="maxila17" name="maxila17" value="17">
<label for="maxila17">17</label>
<input type="checkbox" class="chkbox" id="maxila16" name="maxila16" value="16">
<label for="maxila16">16</label>
<input type="checkbox" class="chkbox" id="maxila15" name="maxila15" value="15">
<label for="maxila15"> 15</label>
<input type="checkbox" class="chkbox" id="maxila14" name="maxila14" value="14">
<label for="maxila14"> 14</label>
<input type="checkbox" class="chkbox" id="maxila13" name="maxila13" value="13">
<label for="maxila13"> 13</label>
<input type="checkbox" class="chkbox" id="maxila12" name="maxila12" value="12">
<label for="maxila12"> 12</label>
<input type="checkbox" class="chkbox" id="maxila11" name="maxila11" value="11">
<label for="maxila11"> 11</label>
<input type="checkbox" class="chkbox" id="maxila21" name="maxila21" value="21">
<label for="maxila21"> 21</label>
<input type="checkbox" class="chkbox" id="maxila22" name="maxila22" value="22">
<label for="maxila22"> 22</label>
<input type="checkbox" class="chkbox" id="maxila23" name="maxila23" value="23">
<label for="maxila23"> 23</label>
<input type="checkbox" class="chkbox" id="maxila24" name="maxila24" value="24">
<label for="maxila24"> 24</label>
<input type="checkbox" class="chkbox" id="maxila25" name="maxila25" value="25">
<label for="maxila25"> 25</label>
<input type="checkbox" class="chkbox" id="maxila26" name="maxila26" value="26">
<label for="maxila26"> 26</label>
<input type="checkbox" class="chkbox" id="maxila27" name="maxila27" value="27">
<label for="maxila27"> 27</label>
<input type="checkbox" class="chkbox" id="maxila28" name="maxila28" value="28">
<label for="maxila28"> 28</label>
<br>
<input type="checkbox" class="chkbox" id="mandible38" name="mandible38" value="38">
<label for="mandible3"> 38</label>
<input type="checkbox" class="chkbox" id="mandible37" name="mandible37" value="37">
<label for="mandible37">37</label>
<input type="checkbox" class="chkbox" id="mandible36" name="mandible36" value="36">
<label for="mandible36">36</label>
<input type="checkbox" class="chkbox" id="mandible35" name="mandible35" value="35">
<label for="mandible35"> 35</label>
<input type="checkbox" class="chkbox" id="mandible34" name="mandible34" value="34">
<label for="mandible34"> 34</label>
<input type="checkbox" class="chkbox" id="mandible33" name="mandible33" value="33">
<label for="mandible33"> 33</label>
<input type="checkbox" class="chkbox" id="mandible32" name="mandible32" value="32">
<label for="mandible32"> 32</label>
<input type="checkbox" class="chkbox" id="mandible31" name="mandible31" value="31">
<label for="mandible31"> 31</label>
<input type="checkbox" class="chkbox" id="mandible41" name="mandible41" value="41">
<label for="mandible43"> 41</label>
<input type="checkbox" class="chkbox" id="mandible42" name="mandible42" value="42">
<label for="mandible42"> 42</label>
<input type="checkbox" class="chkbox" id="mandible43" name="mandible43" value="43">
<label for="mandible43"> 43</label>
<input type="checkbox" class="chkbox" id="mandible44" name="mandible44" value="44">
<label for="mandible44"> 44</label>
<input type="checkbox" class="chkbox" id="mandible45" name="mandible45" value="45">
<label for="mandible45"> 45</label>
<input type="checkbox" class="chkbox" id="mandible46" name="mandible46" value="46">
<label for="mandible46"> 46</label>
<input type="checkbox" class="chkbox" id="mandible47" name="mandible47" value="47">
<label for="mandible47"> 47</label>
<input type="checkbox" class="chkbox" id="mandible48" name="mandible48" value="48">
<label for="mandible48"> 48</label>
<br>
</div>
<div>
<h3> numar elemente selectate</h3>
<div id="boxesChecked"> </div>
</div>
<input type="button" value="ADD CAPE" id="transfercape">!Cape CrCo(4euro) </input>
<input type="button" value="ADD Bonturi" id="transferbonturi">!Bonturi(40euro) </input>
<div id="container1">
<form>
<input type="text" / id="elements" placeholder="elements">
<input type="text" / id="productID" placeholder="productID">
<input type="text" / id="productprice" placeholder="productprice">
<input type="text" / id="selectionprice" placeholder="selectionprice">
<input type="text" / id="teeth" name="teeth " placeholder="teeth" value="">
</form>
</div>
This solved practically main problem, still I can't figure how to replace join separator between consecutive selected boxes from comma to minus.
String returned by .map() function need to conditionally change separator for consecutively checked boxes.
Example:
Checked: 18 14 13 12 24
Now the result is: 18,14,13,12,24
but I need to be: 18,14-13-12,24
Please help!

Append error message only when radio button is not checked on button click

I am trying to show an error if a user presses submit and doesn't check one of the radio buttons. That part works fine. Now, my issue is that even when the radio button is checked and the user presses the button.. the error shows right before the form submits. Why is that error showing when the radio button is checked? Any help will be appreciated.
HTML:
<form action="javascript:void(0);">
<input type="submit" class="add-cart " value="Add">
<br />
<p class="option-name">Size : </p>
<input type="radio" required="required" aria-required="true" id="product1" value="1" name="options[]" />
<label class="label" for="product1">Small</label>
<input type="radio" required="required" aria-required="true" id="product3" value="3" name="options[]" />
<label class="label" for="product3">Medium</label>
<input type="radio" required="required" aria-required="true" id="product4" value="4" name="options[]" />
<label class="label" for="product4">Large</label>
<input type="radio" required="required" aria-required="true" id="product5" value="5" name="options[]" />
<label class="label" for="product5">X-Large</label>
</form>
JS:
$(document).ready(function() {
var count = 0;
$(".add-cart").click(function() {
if (!$("input[name='options']").is(':checked') && count <= 0) {
$(".option-name").append("<p class='option-error'>This field is required</p>");
count++;
}
});
});
JSFiddle Demo
You're not currently resetting between uses, here's a new version:
$(document).ready(function() {
$(".add-cart").click(function() {
$(".option-error").html("");
if (!$("input[name='options[]']:checked").val()) {
$(".option-error").html("This field is required");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="javascript:void(0);">
<input type="submit" class="add-cart " value="Add">
<br />
<p class="option-name">Size : <p class="option-error"></p></p>
<input type="radio" required="required" aria-required="true" id="product1" value="1" name="options[]" />
<label class="label" for="product1">Small</label>
<input type="radio" required="required" aria-required="true" id="product3" value="3" name="options[]" />
<label class="label" for="product3">Medium</label>
<input type="radio" required="required" aria-required="true" id="product4" value="4" name="options[]" />
<label class="label" for="product4">Large</label>
<input type="radio" required="required" aria-required="true" id="product5" value="5" name="options[]" />
<label class="label" for="product5">X-Large</label>
</form>

Javascript classlist object and query selector to toggle

Can someone help me, because I'm stuck right now. I have to make a form toggle betweeen two fieldsets using classlist object and queryselector. I have to use a function in Javascript that adds the classlist to the fieldset.
Right now, this is my code in javascript:
var click = document.querySelector('form>fieldset>label>input[type="radio"]');
var project = document.querySelector(".project");
var stage = document.querySelector(".stage");
click.addEventListener("click", function() {
if (click === "stage") {
project.classList.toggle(".project");
}
else {
stage.classList.toggle(".stage");
}
});
This is my code in HTML:
<fieldset>
<legend>Ik wil mij aanmelden:</legend>
<label>
<input type="radio" name="submit-for" value="project">
<span>Voor een project</span>
</label>
<label>
<input type="radio" name="submit-for" value="stage">
<span>Als stagebedrijf</span>
</label>
</fieldset>
<fieldset id="project" class="project">
<legend>Project</legend>
<label>
<span>Titel:</span>
<input type="text" name="project-title">
</label>
<label>
<span>Opdrachtomschrijving:</span>
<textarea name="project-description"></textarea>
</label>
<label>
<span>Doelgroepomschrijving:</span>
<textarea name="project-target-audience"></textarea>
</label>
<label>
<span>Doelstelling/intentie van het project:</span>
<textarea name="project-goal"></textarea>
</label>
<label>
<span>Looptijd:</span>
<input type="text" name="project-duration">
</label>
<label>
<span>Deadline:</span>
<input type="date" name="project-deadline">
</label>
<fieldset>
<legend>Geschikt voor</legend>
<label><input type="checkbox" name="project-eerstejaars"> Eerstejaars studenten</label>
<label><input type="checkbox" name="project-tweedejaars"> Tweedejaars studenten</label>
<label><input type="checkbox" name="project-derdejaars"> Derdejaars studenten</label>
<label><input type="checkbox" name="project-afstudeer"> Afstudeer studenten</label>
<label><input type="checkbox" name="project-onbekend"> Onbekend</label>
</fieldset>
<label>
<span>Opmerking:</span>
<textarea name="project-comments"></textarea>
</label>
</fieldset>
<fieldset id="stage" class="stage">
<legend>Stage</legend>
<fieldset>
<legend>Geschikt voor</legend>
<label><input type="checkbox" name="stage-tweedejaars"> Tweedejaars studenten</label>
<label><input type="checkbox" name="stage-afstudeerders"> Afstudeer studenten</label>
<label><input type="checkbox" name="stage-onbekend"> Onbekend</label>
</fieldset>
<fieldset>
<legend>Hoe lang is de stage</legend>
<label><input type="radio" name="stage-duration" value="10"> 10 weken</label>
<label><input type="radio" name="stage-duration" value="20"> 20 weken</label>
</fieldset>
<label>
<span>Begindatum:</span>
<input type="date" name="stage-startdate">
</label>
<label>
<span>Beschrijving stagewerkzaamheden</span>
<textarea name="stage-description"></textarea>
</label>
<fieldset>
<legend>Beschrijving stagebedrijf</legend>
<label>
<span>Bedrijfsnaam:</span>
<input type="text" name="stage-company-name">
</label>
<label>
<span>Adres:</span>
<input type="text" name="stage-address">
</label>
<label>
<span>Postcode:</span>
<input type="text" name="stage-postal">
</label>
<label>
<span>Plaats:</span>
<input type="text" name="stage-place">
</label>
<label>
<span>Sector:</span>
<input type="text" name="stage-sector">
</label>
<label>
<span>Core business:</span>
<input type="text" name="stage-core-business">
</label>
</fieldset>
<label>
<span>Opmerking:</span>
<textarea name="stage-comments"></textarea>
</label>
</fieldset>
<input type="submit" value="Aanmelden">
</form>
And this is my code in CSS:
.project {
display: none;
}
.stage {
display: none;
}
The fieldsets called "Project" and "Stage" have to toggle. So, when I click on the radio button called project, the stage form has to disappear, vice versa.
I can't use onclick, because my teacher don't want me to use that.
You have some problems in your code:
You fogot the start form tag.
project.classList.toggle(".project"); need to be project.classList.toggle("project");
2.1 You can't use toggle classes in this case, because you don't want to toggle, you want to add and remove. You want to show just one section on the same time. right?
You have multiple checkboxes so you need to attach click event to each of them.
var click = document.querySelectorAll('form fieldset:first-child>label>input[type="radio"]');
var project = document.querySelector(".project");
var stage = document.querySelector(".stage");
for (var cl = 0; cl < click.length; cl++) {
click[cl].addEventListener("change", function() {
//if (this.value === "stage") {
project.classList.toggle("show");
project.classList.toggle("hide");
stage.classList.toggle("show");
stage.classList.toggle("hide");
//stage.classList.remove("hide");
//}
//else {
// project.classList.remove("hide");
// stage.classList.add("hide");
//}
});
}
.hide {
display:none;
}
<form>
<fieldset>
<legend>Ik wil mij aanmelden:</legend>
<label>
<input type="radio" name="submit-for" value="project" checked="checked">
<span>Voor een project</span>
</label>
<label>
<input type="radio" name="submit-for" value="stage">
<span>Als stagebedrijf</span>
</label>
</fieldset>
<fieldset id="project" class="project hide">
<legend>Project</legend>
<label>
<span>Titel:</span>
<input type="text" name="project-title">
</label>
<label>
<span>Opdrachtomschrijving:</span>
<textarea name="project-description"></textarea>
</label>
<label>
<span>Doelgroepomschrijving:</span>
<textarea name="project-target-audience"></textarea>
</label>
<label>
<span>Doelstelling/intentie van het project:</span>
<textarea name="project-goal"></textarea>
</label>
<label>
<span>Looptijd:</span>
<input type="text" name="project-duration">
</label>
<label>
<span>Deadline:</span>
<input type="date" name="project-deadline">
</label>
<fieldset>
<legend>Geschikt voor</legend>
<label><input type="checkbox" name="project-eerstejaars"> Eerstejaars studenten</label>
<label><input type="checkbox" name="project-tweedejaars"> Tweedejaars studenten</label>
<label><input type="checkbox" name="project-derdejaars"> Derdejaars studenten</label>
<label><input type="checkbox" name="project-afstudeer"> Afstudeer studenten</label>
<label><input type="checkbox" name="project-onbekend"> Onbekend</label>
</fieldset>
<label>
<span>Opmerking:</span>
<textarea name="project-comments"></textarea>
</label>
</fieldset>
<fieldset id="stage" class="stage">
<legend>Stage</legend>
<fieldset>
<legend>Geschikt voor</legend>
<label><input type="checkbox" name="stage-tweedejaars"> Tweedejaars studenten</label>
<label><input type="checkbox" name="stage-afstudeerders"> Afstudeer studenten</label>
<label><input type="checkbox" name="stage-onbekend"> Onbekend</label>
</fieldset>
<fieldset>
<legend>Hoe lang is de stage</legend>
<label><input type="radio" name="stage-duration" value="10"> 10 weken</label>
<label><input type="radio" name="stage-duration" value="20"> 20 weken</label>
</fieldset>
<label>
<span>Begindatum:</span>
<input type="date" name="stage-startdate">
</label>
<label>
<span>Beschrijving stagewerkzaamheden</span>
<textarea name="stage-description"></textarea>
</label>
<fieldset>
<legend>Beschrijving stagebedrijf</legend>
<label>
<span>Bedrijfsnaam:</span>
<input type="text" name="stage-company-name">
</label>
<label>
<span>Adres:</span>
<input type="text" name="stage-address">
</label>
<label>
<span>Postcode:</span>
<input type="text" name="stage-postal">
</label>
<label>
<span>Plaats:</span>
<input type="text" name="stage-place">
</label>
<label>
<span>Sector:</span>
<input type="text" name="stage-sector">
</label>
<label>
<span>Core business:</span>
<input type="text" name="stage-core-business">
</label>
</fieldset>
<label>
<span>Opmerking:</span>
<textarea name="stage-comments"></textarea>
</label>
</fieldset>
<input type="submit" value="Aanmelden">
</form>

Update price on two checkbox selection with javascript or jQuery

I need a way to show correct price for a product with selection options. Radio Box 1 has 4 products with different price ProductA=$10, ProductB=$20, ProductC=$30, ProductD=$40
Radio Box 2 has two options Male and Female
For female all products are always $10 regardless of Radio Box 1 selection. Price varies for men depending on product choice. I need to show price change dynamically with js.
http://jsfiddle.net/pe2gpp01/
<div><label class="product">Product</label>
<span>
<input name="category" type="radio" value="10">
<label>Product A</label>
<input name="category" type="radio" value="20">
<label>Product B</label>
<input name="category" type="radio" value="30">
<label>Product C</label>
<input name="category" type="radio" value="40">
<label>Product D</label>
</span></div>
<div>
<label class="gender">Gender</label>
<span>
<input name="gender" type="radio" value="">
<label>Male</label>
<input name="gender" type="radio" value="">
<label>Female</label>
</span></div>
<span>Price:</span>
DEMO
html
<div>
<label class="product">Product</label>
<span>
<input name="category" type="radio" value="10" >
<label>Product A</label>
<input name="category" type="radio" value="20" checked>
<label>Product B</label>
<input name="category" type="radio" value="30">
<label>Product C</label>
<input name="category" type="radio" value="40" >
<label>Product D</label>
</span></div>
<div>
<label class="gender">Gender</label>
<span>
<input name="gender" type="radio" value="male" checked>
<label>Male</label>
<input name="gender" type="radio" value="female">
<label>Female</label>
</span></div>
<span>Show Price: <span id="price"></span></span>
js
$(function() {
$('[type="radio"]').on('change', function() {
var price = $('[value="female"]')[0].checked
? 10
: $('[name="category"]:checked').val();
$('#price').text(price);
}).change();
});

Categories

Resources