$('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);
});
Related
Given this checkbox html element:
<h4>This is a question?</h4>
<input type="radio" value="1" name="q">answer 1</input>
<input type="radio" value="2" name="q">answer 2</input>
<input type="radio" value="3" name="q">answer 3</input>
How can I lock the state of a checkbox after it has been selected? That is, how can I avoid the user to change his answer?
A hacky idea using CSS. You make a layer that will prevent any click event when one input is checked
.block {
position:relative;
}
.block input:checked ~ i {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
<div class="block">
<h4>This is a question? (blocked)</h4>
<input type="radio" value="1" name="q1">answer 1
<input type="radio" value="2" name="q1">answer 2
<input type="radio" value="3" name="q1">answer 3
<i></i>
</div>
<div >
<h4>This is a question? (not blocked)</h4>
<input type="radio" value="1" name="q2">answer 1
<input type="radio" value="2" name="q2">answer 2
<input type="radio" value="3" name="q2">answer 3
<i></i>
</div>
With Anush Bhatia's example, you would need to also use JQuery.
Here is a simple snippet without the use of JQuery:
function change(yourname) {
var radios = document.getElementsByName(youname);
for (var i=0, iLen=radios.length; i<iLen; i++) {
radios[i].disabled = true;
}
}
Remember that you should also call the function on change using
<input type="radio" value="1" onchange="change(\"q\");" name="q">answer 1</input>
$('input:radio').click(function(){
var $inputs = $('input:radio')
if($(this).is(':radio')){
$inputs.not(this).prop('disabled',true); // <-- disable all but checked one
}else{
$inputs.prop('disabled',false); // <--
}
})
Try using this.
Refer to the link below http://jsfiddle.net/ur9zxo2e/
You could make each question be in ONE element and apply a questionHandle function to each question element
var questionElement1=document.getElementById('q1')
function questionHandle(elem){
var answered=false
function listen(ev){
if(answered){return ev.preventDefault()}
//else, since this only happens IF not pressed already
answered=true //so that it will not be pressed again
//any other thing you want to do for when question is answered below after this INSIDE this function
console.log(`question ${ev.path[0].value} was chosen`)//an example of anything you can do
}
try{
[...elem.children]
.forEach(a=>a.addEventListener('click',listen))
}
catch(err){return Error(err)}
}
questionHandle(questionElement1)
<h4>This is a question?</h4>
<div id="q1">
<input type="radio" value="1" name="q">answer 1</input>
<input type="radio" value="2" name="q">answer 2</input>
<input type="radio" value="3" name="q">answer 3</input>
</div>
I will do that this way :
const myForm = document.forms['my-form']
myForm.radioChoice = {}
myForm.oninput = ({target}) =>
{
if( target.type === 'radio')
{
if (!myForm.radioChoice[target.name])
myForm.radioChoice[target.name] = target.value
else
myForm[target.name].value = myForm.radioChoice[target.name]
}
}
<form action="" name="my-form">
<fieldset>
<legend>Question 1</legend>
<label><input type="radio" value="1" name="q1" />answer 1</label>
<label><input type="radio" value="2" name="q1" />answer 2</label>
<label><input type="radio" value="3" name="q1" />answer 3</label>
</fieldset>
<fieldset>
<legend>Question 2</legend>
<label><input type="radio" value="1" name="q2" />answer 1</label>
<label><input type="radio" value="2" name="q2" />answer 2</label>
<label><input type="radio" value="3" name="q2" />answer 3</label>
</fieldset>
</form>
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>
i am trying to add url parameter like ( www.example.com/product#green) this but i need multiple paramater like ( www.example.com/product#green#large ) after first parameter add size i wrote something but it doesn't work properly. But when user select size first should push parameter. When user click colour then size www.example.com/product#green#large
And i have another problem when i refresh the page the parameters stay there.I need to clear url parameters.I added jsFiddle link down below.
How can i do this with jquery.
var currentUrl = window.location.href;
$("#colour-stage label").click(function() {
var valColourInput = $('input[name=colour]:checked').val();
document.location.href = currentUrl + '#' + valColourInput;
});
$("#size-stage label").click(function() {
var valSizeInput = $('input[name=size]:checked').val();
document.location.href = currentUrl + '#' + valSizeInput;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="get">
<fieldset id="colour-stage">
<legend>
Colours:
</legend>
<label>
<input type="radio" name="colour" value="Red">
<span>Red</span>
</label>
<label>
<input type="radio" name="colour" value="Blue">
<span>Blue</span>
</label>
<label>
<input type="radio" name="colour" value="Yellow">
<span>Yellow</span>
</label>
<label>
<input type="radio" name="colour" value="Green">
<span>Green</span>
</label>
</fieldset>
<fieldset id="size-stage">
<legend>
Size:
</legend>
<label>
<input type="radio" name="size" value="Small">
<span>Small</span>
</label>
<label>
<input type="radio" name="size" value="medium">
<span>Medium</span>
</label>
<label>
<input type="radio" name="size" value="large">
<span>Large</span>
</label>
</fieldset>
</form>
https://jsfiddle.net/tolga748/06ekvf72/
I have two checkbox and under this when tick one of two checkbox I have two another option checkbox and another tick there will be text. The text will be value from the checkbox, how can I get the value from this checkbox? But the text I want to put are different sentence for each option checkbox selected not the word from checkbox. Did anyone know?
And my another problem is the checkbox not working if I do not tick the first checkbox.
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" /> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" /> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
Add value attribute to the checkbox. and set value to the checkbox.Or you can take the text from the label
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
console.log(Cat.value)
console.log("Text Inside LABEL:" + Cat.parentNode.textContent )
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" value="cat"/> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
console.log(Rabbit.value)
console.log("Text Inside LABEL:" + Rabbit.parentNode.textContent )
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" value="Rabbit"/> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
</div>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
I think you can follow this simple logic:
https://jsfiddle.net/pablodarde/6p7zkfwf/
HTML
<div class="container">
<div class="row">
<input type="checkbox" value="This is option one!" id="opt1"><label for="opt1">Option One</label>
<p></p>
</div>
<div class="row">
<input type="checkbox" value="This is option two!" id="opt2"><label for="opt2">Option Two</label>
<p></p>
</div>
</div>
JavaScript
const checkboxes = document.querySelectorAll('.container input[type=checkbox]');
for (let i = 0, l = checkboxes.length; i < l; i++) {
checkboxes[i].addEventListener('click', (e) => {
if(checkboxes[i].parentNode.querySelector('p').innerHTML == "") {
checkboxes[i].parentNode.querySelector('p').innerHTML = checkboxes[i].value;
} else {
checkboxes[i].parentNode.querySelector('p').innerHTML = "";
}
});
}
CSS
.row {
padding-bottom: 10px;
border-bottom: 1px solid black;
}
There are two separate divs: check_one & check_two.
check_one has 4 boxes in it, check_two has 1.
If you check the checkbox within the check_two div, it should uncheck all chekced boxes within the first div, check_one. That's sort of confusing, but that is what I'm attempting to do.
HTML ::
<div id="check_one">
<label>Stuff 1</label> <input name="" type="checkbox" value="" id="group1" checked="checked"><br>
<label>Stuff 2</label> <input name="" type="checkbox" value="" id="group1" checked="checked"><br>
<label>Stuff 3</label> <input name="" type="checkbox" value="" id="group1" checked="checked"><br>
<label>Stuff 4</label> <input name="" type="checkbox" value="" id="group1" checked="checked">
</div>
<br>
<div id="check_two">
<label>Undo</label> <input name="uncheckMe" type="checkbox" value="" id="group2">
</div>
jQuery ::
$('input[name=uncheckMe]').change(
{
var checked = $("#group1").attr("checked");
if(checked)
{
$("#check_two").attr("disabled", true);
}
});
I created a jsFiddle for this http://jsfiddle.net/Twisty/ta2qT/
You're missing a Function within Change():
$('input[name=uncheckMe]').change(function(){
$("#check_one > input").each(function(i) {
if ($(this).is(":checked")) {
$(this).removeAttr("checked");
}
});
});
Try this:
$("input[name='uncheckMe']").change(function(){
if ($(this).is(":checked")){
$("#check_one input").removeAttr("checked");
}
});
And a jsFiddle: http://jsfiddle.net/PgHbu/3/