jQuery: cannot conditionally add class to children elements - html

I am trying to conditionally add the class "closed-3" to all the children "input" elements of an ancestor. The ancestor should contain the classes ".summon-next-element.on-sub-sub-level". However, I get no effect - no class added, no error in the console, nothing. Here is the jQuery:
$('.summon-next-element.on-sub-sub-level').each(function() {
if ($(this).find("input").is(":checked")) {
$(this).nextAll(".summoned-element").first().find("input").addClass("opened-3").removeClass("closed-3");
} else {
$(this).nextAll(".summoned-element").first().find("input").addClass("closed-3").removeClass("opened-3");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="summon-next-element on-sub-sub-level">
<input type="radio" name="frais_renovation" id="frais_renovation_oui" class="oui">
<label for="frais_renovation_oui">oui</label>
</div>
<div class="checkbox">
<input type="radio" name="frais_renovation" id="frais_renovation_non" class="non">
<label for="frais_renovation_non">non</label>
</div>
<div class="summoned-element">
<div class="checkbox info-circle right">
<input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_fichier">
<label for="frais_renovation_fichier">
Mes justificatifs 1
</label>
</div>
<div class="checkbox info-circle right">
<input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_tickets" checked>
<label for="frais_renovation_tickets">
Mes justificatifs 2
</label>
</div>
</div>
<!-- end of summoned element -->
Thank you so much for your help!

It is also necessary to wrap your logic in the radio button selection event, according to which the reduced assignment / deletion of classes will take place:
$(this).on('click', function() { ... }
In your code, there was also an incorrect class reference.
$('div').find('input[type=radio]').each(function() {
$(this).on('click', function() {
$('input[type=radio]').addClass("closed-3").removeClass("opened-3");
if ($(this).is(":checked")) {
$(this).addClass("opened-3").removeClass("closed-3");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="summon-next-element on-sub-sub-level">
<input type="radio" name="frais_renovation" id="frais_renovation_oui" class="oui">
<label for="frais_renovation_oui">oui</label>
</div>
<div class="checkbox">
<input type="radio" name="frais_renovation" id="frais_renovation_non" class="non">
<label for="frais_renovation_non">non</label>
</div>
<div class="summoned-element">
<div class="checkbox info-circle right">
<input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_fichier">
<label for="frais_renovation_fichier">
Mes justificatifs 1
</label>
</div>
<div class="checkbox info-circle right">
<input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_tickets" checked>
<label for="frais_renovation_tickets">
Mes justificatifs 2
</label>
</div>
</div>
<!-- end of summoned element -->

Problem is that you are not looping over each of the inputs, so when you check if the input is checked, it will always select the first inside .on-sub-sub-level.
Try with:
$('.summon-next-element.on-sub-sub-level ~ .summoned-element input').each(function() {
if ($(this).is(":checked")) {
$(this).addClass("opened-3").removeClass("closed-3");
} else {
$(this).addClass("closed-3").removeClass("opened-3");
}
});
Demo
$('.summon-next-element.on-sub-sub-level ~ .summoned-element input').each(function() {
if ($(this).is(":checked")) {
$(this).addClass("opened-3").removeClass("closed-3");
} else {
$(this).addClass("closed-3").removeClass("opened-3");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="summon-next-element on-sub-sub-level">
<input type="radio" name="frais_renovation" id="frais_renovation_oui" class="oui">
<label for="frais_renovation_oui">oui</label>
</div>
<div class="checkbox">
<input type="radio" name="frais_renovation" id="frais_renovation_non" class="non">
<label for="frais_renovation_non">non</label>
</div>
<div class="summoned-element">
<div class="checkbox info-circle right">
<input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_fichier">
<label for="frais_renovation_fichier">
Mes justificatifs 1
</label>
</div>
<div class="checkbox info-circle right">
<input type="radio" name="frais_renovation_justificatifs" id="frais_renovation_tickets" checked>
<label for="frais_renovation_tickets">
Mes justificatifs 2
</label>
</div>
</div>
<!-- end of summoned element -->

Related

Using only CSS to change the background color of the answer choosen

I want to change the color of the answer when player click to answer box (to choose it) like the picture below.
Here is my html code, the radio box is a child of label. I tried some commands such as:
.choices > input[type="radio"]:checked {background: yellow;}
but seemingly it's not true. And someone please tell me the differences when I put unchecked="checked" into the radio-box line?
Thank all of you so much!
<!-- ---question1--- -->
<div class="quest">
<h1>Question 1 of 10</h1>
<h5>Question 1</h5>
<div class="group">
<label for="opt1" class="choices">
<input type="radio" id="1" name="q1" value="o1" >Option 1<br>
</label>
<label for="opt2" class="choices">
<input type="radio" id="2" name="q1" value="o2" >Option 2<br>
</label>
<label for="opt3" class="choices">
<input type="radio" id="3" name="q1" value="o3" >Option 3<br>
</label>
<label for="opt4" class="choices">
<input type="radio" id="4" name="q1" value="o4" >Option 4<br>
</label>
</div>
</div>
<!-- ---question2--- -->
<div class="quest">
<h1>Question 2 of 10</h1>
<h5>Question 2</h5>
<div class="group">
<label for="opt1" class="choices">
<input type="radio" id="5" name="q2" value="o1" unchecked="checked">Option 1<br>
</label>
<label for="opt2" class="choices">
<input type="radio" id="6" name="q2" value="o2" unchecked="checked">Option 2<br>
</label>
<label for="opt3" class="choices">
<input type="radio" id="7" name="q2" value="o3" unchecked="checked">Option 3<br>
</label>
<label for="opt4" class="choices">
<input type="radio" id="8" name="q2" value="o4" unchecked="checked">Option 4<br>
</label>
</div>
</div>
Some have already pointed out that checked + label is a possible solution. Here is the example, but somewhat shortened.
input[type="radio"]:checked + label {
background: yellow;
}
<!-- ---question1--- -->
<div class="quest">
<h1>Question 1 of 10</h1>
<h5>Question 1</h5>
<div class="group">
<div>
<input type="radio" id="1" name="q1" value="o1">
<label for="1"> Option 1 </label>
</div>
<div>
<input type="radio" id="2" name="q1" value="o2">
<label for="2"> Option 2</label>
</div>
</div>
</div>
It's a bit tricky with CSS only but not impossible. I changed a bit the HTML structure but IT WORKS.
The unchecked attribute doesn't exist, you should only use <input type="radio" id="opt1" name="q1" value="o1"> and add checked for the selected state like in the example below.
.choices {
position: relative;
}
.choices > input[type="radio"] {
position: absolute;
left: 4px;
top: 4px;
}
.choices label {
padding: 4px 32px;
display: block;
}
.choices > input[type="radio"]:checked + label {
background: yellow;
}
<div class="quest">
<h1>Question 1 of 10</h1>
<h5>Question 1</h5>
<div class="group">
<div class="choices">
<input type="radio" id="opt1" name="q1" value="o1">
<label for="opt1">Option 1</label>
</div>
<div class="choices">
<input type="radio" id="opt2" name="q1" value="o2">
<label for="opt2">Option 2</label>
</div>
<div class="choices">
<input type="radio" id="opt3" name="q1" value="o3" checked>
<label for="opt3">Option 3</label>
</div>
<div class="choices">
<input type="radio" id="opt4" name="q1" value="o4">
<label for="opt4">Option 4</label>
</div>
</div>
</div>
There are some issues in your code:
The for attribute of the label should be the same as the corresponding input's id
<div>
<input type="radio" id="5" name="q1" value="o1">
<label for="5"> Option 1 </label>
</div>
There's NO unchecked attribute in HTML for radio!
We only have checked attribute. See MDN Docs and this question for more info.
Styling
Since you cannot select the parent element in CSS, you can your write code in the format I mentioned above and style the label of the input.
input[type="radio"]:checked + label {
background: yellow;
}
Otherwise, you need to use js: if checkbox checked add class to parent element
You Can handle it in JScript, If you go to CSS documentation the css code might not work on old browsers:
var elements = document.getElementsByClassName("answer");
for (var i = 0; i < elements.length; i++) {
var element1 = elements[i];
element1.addEventListener('click', function(element1){
this.firstElementChild.className="selected"
}, false);
element1.addEventListener('focusout', function(element1){
this.firstElementChild.className="area";
this.firstElementChild.firstElementChild.checked=false;
}, false);
}
.answer{margin-top:30px;}
.selected{background:yellow;padding:10px;}
.area{
background:red;
padding:10px;
}
.area:active{background:yellow;}
<!DOCTYPE html>
<html>
<head>
<title>HTML CSS JS</title>
</head>
<body>
<div class="Question_box">
1. Question is to pick one.
</div>
</br>
<div class="answer">
<label class="area">
<input type="radio" name="data" id="1" class="radio"/> Option 1
</label>
</div>
<div class="answer">
<label class="area">
<input type="radio" name="data" id="2" class="radio"/> Option 2
</label>
</div>
<div class="answer">
<label class="area">
<input type="radio" name="data" id="3" class="radio"/> Option 3
</label>
</div>
<div class="answer">
<label class="area">
<input type="radio" name="data"id="4" class="radio"/> Option 4
</label>
</div>
</div>
</body>
</html>
you need to change your mark up and little bit CSS.
main css will be
input[type="radio"]:checked+.choices {background: yellow;}
instated of this
<label for="opt1" class="choices">
<input type="radio" id="5" name="q2" value="o1" unchecked="checked">Option 1<br>
</label>
you need to write like this for all inputs,
<input type="radio" id="5" name="q2" value="o1" unchecked="checked">
<label for="opt1" class="choices">Option 1</label>
l don't know your second question but I think there is nothing to think about it.

How to align three divs containing radio inputs

I have three divs in a form that accept radio buttons as input. Currently with my css it looks like this:
Please see this link
O Text O Text O Text
O Text O Text&Text O Text
O Text O Text
How can I edit my css to make it so that the radio buttons are vertically aligned AND the whole set of radio inputs center aligned , something like this:
O Text O Text O Text
O Text O Text&Text O Text
O Text O Text
This is the code I have
Thank you!
<div class="form-group">
<label for="material" class="text-light">Text</label>
<div class="radio inline">
<input type="radio" id="plastic" name="material" value="Plastic">
<label for="plastic" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="cans" name="material" value="Cans">
<label for="cans" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="paper" name="material" value="Paper">
<label for="paper" class="radio-label">Text</label>
</div>
</div>
<div class="form-group">
<label for="relationship" class="text-light">Text</label>
<div class="radio inline">
<input type="radio" id="school" name="relationship" value="School">
<label for="school" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="family" name="relationship" value="Friends & Family">
<label for="family" class="radio-label">TextText</label>
</div>
<div class="radio inline">
<input type="radio" id="company" name="relationship" value="Company">
<label for="company" class="radio-label">Company</label>
</div>
</div>
<div class="form-group">
<label for="frequency" class="text-light">Type</label>
<div class="radio inline">
<input type="radio" class="freq" name="frequency" id="Repeating" value="Repeating">
<label for="Repeating" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" class="freq" name="frequency" id="One-Time" value="One-Time">
<label for="One-Time" class="radio-label">Text</label>
</div>
If you wrap these form-groups in a container, you can set that container to display: flex which will allow you to use flexbox properties to align your form groups as needed.
<div class="radio-container">
<fieldset class="form-group">
<legend id="material" class="text-light">Text</legend>
<div class="radio inline">
<input type="radio" id="plastic" name="material" value="Plastic">
<label for="plastic" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="cans" name="material" value="Cans">
<label for="cans" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="paper" name="material" value="Paper">
<label for="paper" class="radio-label">Text</label>
</div>
</fieldset>
<fieldset class="form-group">
<legend id="relationship" class="text-light">Text</legend>
<div class="radio inline">
<input type="radio" id="school" name="relationship" value="School">
<label for="school" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="family" name="relationship" value="Friends & Family">
<label for="family" class="radio-label">TextText</label>
</div>
<div class="radio inline">
<input type="radio" id="company" name="relationship" value="Company">
<label for="company" class="radio-label">Company</label>
</div>
</fieldset>
<fieldset class="form-group">
<legend id="frequency" class="text-light">Type</legend>
<div class="radio inline">
<input type="radio" class="freq" name="frequency" id="Repeating" value="Repeating">
<label for="Repeating" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" class="freq" name="frequency" id="One-Time" value="One-Time">
<label for="One-Time" class="radio-label">Text</label>
</div>
</fieldset>
</div>
.radio-container{
display: flex;
justify-content: center;
}
You can use display:flex along with a width:Xpx to create aligned rows. Then justify-content: center; on the whole flex container to center it. If you want the title label text to align with the radio label text you can set the radios to absolute postion and put a common padding on all labels
.container {
display: flex;
margin: auto;
justify-content: center;
}
.radio.inline {
width: 100px;
}
label {
padding-left: 25px;
}
input[type='radio'] {
position: absolute;
}
<div class="container">
<div class="form-group">
<label for="material" class="text-light">Text</label>
<div class="radio inline">
<input type="radio" id="plastic" name="material" value="Plastic">
<label for="plastic" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="cans" name="material" value="Cans">
<label for="cans" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="paper" name="material" value="Paper">
<label for="paper" class="radio-label">Text</label>
</div>
</div>
<div class="form-group">
<label for="relationship" class="text-light">Text</label>
<div class="radio inline">
<input type="radio" id="school" name="relationship" value="School">
<label for="school" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" id="family" name="relationship" value="Friends & Family">
<label for="family" class="radio-label">TextText</label>
</div>
<div class="radio inline">
<input type="radio" id="company" name="relationship" value="Company">
<label for="company" class="radio-label">Company</label>
</div>
</div>
<div class="form-group">
<label for="frequency" class="text-light">Type</label>
<div class="radio inline">
<input type="radio" class="freq" name="frequency" id="Repeating" value="Repeating">
<label for="Repeating" class="radio-label">Text</label>
</div>
<div class="radio inline">
<input type="radio" class="freq" name="frequency" id="One-Time" value="One-Time">
<label for="One-Time" class="radio-label">Text</label>
</div>
</div>

Symfony - EntityType rendering

I have a form which have a field of type EntityType named categories that is showing like this :
each one is placed in the bottom of the other, i want them to be displayed by group of 2 like this :
anyone have an idea how to do that with Symfony and Twig ?
EDIT :
here is the html code produced by the twig engine for that field
<form name="form" method="post">
<button type="submit" name="rechercher" class="btn btn-success">Rechercher</button>
<div id="form">
<div class="form-group">
<label class="control-label" for="form_motcle">Mot Clé</label>
<input type="text" id="form_motcle" name="form[motcle]" class="form-control" />
</div>
<div class="form-group">
<label class="control-label">Categories</label>
<div id="form_categories">
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_1" name="form[categories][]" value="1" /> Théatre (0)
</label>
</div>
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_2" name="form[categories][]" value="2" /> Cinema (0)
</label>
</div>
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_3" name="form[categories][]" value="3" /> Comédie (0)
</label>
</div>
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_4" name="form[categories][]" value="4" /> Spectacle (0)
</label>
</div>
</div>
</div>
</div>
</form>
You can solve this with a little CSS already. For information and usage of flexbox I always suggest this article.
What this code does is basically the following:
Define #form_categories as somewhat of a container. Elements inside should be placed from left to right (row) and if they reach the end, should use a new line (wrap).
Each div which holds a checkbox (#form_categories > .checkbox) has a width of 50%, so you will have 2 divs per "row".
The advantage of an approach like this is that you can use media queries and change width: 100%; of a single div so it has its own row (e.g. on mobile displays).
#form_categories {
display: flex;
flex-flow: row wrap;
width: 100%;
}
#form_categories > .checkbox {
width: 50%;
}
<div id="form_categories">
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_1" name="form[categories][]" value="1" /> Théatre (0)
</label>
</div>
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_2" name="form[categories][]" value="2" /> Cinema (0)
</label>
</div>
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_3" name="form[categories][]" value="3" /> Comédie (0)
</label>
</div>
<div class="checkbox">
<label class="">
<input type="checkbox" id="form_categories_4" name="form[categories][]" value="4" /> Spectacle (0)
</label>
</div>
</div>

CSS - show div when select is checked

I'm trying to show div when one of the selects in group is checked and hide when not. I thought this should work, but it's not working.
https://jsfiddle.net/47ctm349/2/
#form-group-osobko {
display: none;
}
#odber-1:checked+#form-group-osobko {
display: block;
}
<!-- Multiple Checkboxes -->
<div class="form-group">
<label class="col-md-4 control-label" for="odber">Způsob platby</label>
<div class="col-md-4">
<div class="checkbox">
<label for="odber-0">
<input type="checkbox" name="odber" id="odber-0" value="1"> Dobírka
</label>
</div>
<div class="checkbox">
<label for="odber-1">
<input type="checkbox" name="odber" id="odber-1" value="2"> Hotově při osobním odběru
</label>
</div>
<div class="checkbox">
<label for="odber-2">
<input type="checkbox" name="odber" id="odber-2" value="3"> Platba předem na účet
</label>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group" id="form-group-osobko">
<label class="col-md-4 control-label" for="osobniodber">Místo osobního odběru</label>
<div class="col-md-4">
<input id="osobniodber" name="osobniodber" type="text" placeholder="14900,11000" class="form-control input-md">
<span class="help-block">Zadejte PSČ míst možného osobního odběru oddělená čárkou.</span>
</div>
</div>
It should work when you click the checkbox in the middle, the div id="form-group-osobko" should appear.
I was trying not to use JS with my bootstrap.
Can't get my head around what I'm doing wrong :(
Thanks.
Your solution only works, if the div to be displayed is an adjacent sibling of the checkbox input, as that is what the + selector in CSS means.
If this div is neither a sibling nor a child you can't select it with CSS and will have to use JavaScript.
#form-group-osobko {
display: none;
}
#odber-1:checked+#form-group-osobko {
display: block;
}
<!-- Multiple Checkboxes -->
<div class="form-group">
<label class="col-md-4 control-label" for="odber">Způsob platby</label>
<div class="col-md-4">
<div class="checkbox">
<label for="odber-0">
<input type="checkbox" name="odber" id="odber-0" value="1">Dobírka
</label>
</div>
<div class="checkbox">
<label for="odber-1">
<input type="checkbox" name="odber" id="odber-1" value="2">Hotově při osobním odběru
<!-- Text input-->
<div class="form-group" id="form-group-osobko">
<label class="col-md-4 control-label" for="osobniodber">Místo osobního odběru</label>
<div class="col-md-4">
<input id="osobniodber" name="osobniodber" type="text" placeholder="14900,11000" class="form-control input-md">
<span class="help-block">Zadejte PSČ míst možného osobního odběru oddělená čárkou.</span>
</div>
</div>
</label>
</div>
<div class="checkbox">
<label for="odber-2">
<input type="checkbox" name="odber" id="odber-2" value="3">Platba předem na účet
</label>
</div>
</div>
</div>
Here would be a simple jQuery solution, using just an additional class to display the div:
$('#odber-1').change(function(){
$('#form-group-osobko').toggleClass("active");
});
#form-group-osobko {
display: none;
}
#form-group-osobko.active {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Multiple Checkboxes -->
<div class="form-group">
<label class="col-md-4 control-label" for="odber">Způsob platby</label>
<div class="col-md-4">
<div class="checkbox">
<label for="odber-0">
<input type="checkbox" name="odber" id="odber-0" value="1">Dobírka
</label>
</div>
<div class="checkbox">
<label for="odber-1">
<input type="checkbox" name="odber" id="odber-1" value="2">Hotově při osobním odběru
</label>
</div>
<div class="checkbox">
<label for="odber-2">
<input type="checkbox" name="odber" id="odber-2" value="3">Platba předem na účet
</label>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group" id="form-group-osobko">
<label class="col-md-4 control-label" for="osobniodber">Místo osobního odběru</label>
<div class="col-md-4">
<input id="osobniodber" name="osobniodber" type="text" placeholder="14900,11000" class="form-control input-md">
<span class="help-block">Zadejte PSČ míst možného osobního odběru oddělená čárkou.</span>
</div>
</div>
Get it https://jsfiddle.net/47ctm349/5/
I just added Javascript, using Jquery. It works for any div anywhere:
$('#odber-0').on('change', function(){
if($('#odber-0').is(':checked'))
$("#form-group-osobko").css("display","block");
else
$("#form-group-osobko").css("display","none");
});
$('#odber-1').on('change', function(){
if($('#odber-1').is(':checked'))
$("#form-group-osobko").css("display","block");
else
$("#form-group-osobko").css("display","none");
});
$('#odber-2').on('change', function(){
if($('#odber-2').is(':checked'))
$("#form-group-osobko").css("display","block");
else
$("#form-group-osobko").css("display","none");
});
In your case no ideas with CSS.
Good luck!

Wide space between label for vertical radio button and first choice

Wide space between label for vertical radio button and first choice. In an ideal world, I would love it to be the same amount of space as between the field labels and the top line on the fields. Any thoughts or ideas? Thanks in advance.
http://jsfiddle.net/steveadams617/MgcDU/8543/
<div class="container">
<form>
<div class="row">
<div class="col-sm-6">
<div class='form-group'>
<label for="provider_id">Provider</label>
<div>
<input class="form-control" type="text" maxlength="255" value="" name="provider_id_auto" id="provider_id_auto"/>
</div>
</div>
</div>
<div class="col-sm-3">
<div class='form-group'>
<label for='phone_us'>Phone</label>
<input class='form-control phone_us' type='text' maxlength='14' value='' name='phone_us' id='phone_us'/>
</div>
</div>
</div><!-- .row -->
<div class='row'>
<div class="col-sm-6">
<label for="med_coverage_id">Insurance Coverage</label>
<div class="radio">
<label>
<input type="radio" name="med_coverage_id" value="1" checked />
Pending
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="med_coverage_id" value="2" />
Covered
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="med_coverage_id" value="3" />
Needs Prior Approval
</label>
</div>
</div>
</div><!-- .row -->
</form>
</div><!-- .container -->
Your div .radio has a margin-top of 10px. You can manually adjust this like so
<div class="radio" style="margin-top: 0px;">
<label>
<input type="radio" name="med_coverage_id" value="1" checked="">Pending </label>
</div>
or of course edit the stylesheet or add your own style to the .radio-class (eg if you are using a CDN).