I have 2 columns 'Select' & 'Super' these columns have checkboxes, and can be selected or unselected by the user. What I am looking for is a jQuery solution to :
If any checkbox in Column B (super) is selected then the
corresponding checkbox in Column A (select) should be checked
If any checkbox in Column A (select) is unselected, then the
corresponding checkbox in Column B (super) should be unchecked
The HTML code that I have is:
<td>
<input type="checkbox" id="select1" checked="checked" class="select-checkbox">
</td>
<td><input type="checkbox" id="super1" class="super-checkbox">
</td>
</tr>
<tr>
<td><input type="checkbox" id="select2" class="select-checkbox">
</td>
<td><input type="checkbox" id="super2" class="super-checkbox">
</td>
</tr>
<tr>
<td><input type="checkbox" id="select3" checked="checked" class="select-checkbox">
</td>
<td><input type="checkbox" id="super3" checked="checked" class="super-checkbox">
</td>
</tr>
<tr>
<td><input type="checkbox" id="select4" checked="checked" class="select-checkbox">
</td>
<td><input type="checkbox" id="super4" checked="checked" class="super-checkbox">
</td>
</tr>```
First you need to add a common class to all select checkboxes, and a common class to all super checkboxes
$(function(){
$('.super-checkbox').change(function(e){
const chk = $(this);
if(chk.is(':checked')){
//Super is checked
// get the closest tr
const tr = chk.closest('tr');
//find the select checkbox, under this tr
const selectChk = tr.find('.select-checkbox');
//Check this select checkbox
selectChk.prop('checked',true);
}
});
$('.select-checkbox').change(function(e){
const chk = $(this);
if(!chk.is(':checked')){
//Select is not checked
// get the closest tr
const tr = chk.closest('tr');
//find the super checkbox, under this tr
const selectChk = tr.find('.super-checkbox');
//un-Check this select checkbox
selectChk.prop('checked', false);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input type="checkbox" id="select1" checked="checked" class="select-checkbox">
</td>
<td><input type="checkbox" id="super1" class="super-checkbox">
</td>
</tr>
<tr>
<td><input type="checkbox" id="select2" class="select-checkbox">
</td>
<td><input type="checkbox" id="super2" class="super-checkbox hrpda-interview-super-checkbox">
</td>
</tr>
<tr>
<td><input type="checkbox" id="select3" checked="checked" class="select-checkbox hrpda-interview-select-checkbox">
</td>
<td><input type="checkbox" id="super3" checked="checked" class="super-checkbox">
</td>
</tr>
<tr>
<td><input type="checkbox" id="select4" checked="checked" class="select-checkbox">
</td>
<td><input type="checkbox" id="super4" checked="checked" class="super-checkbox">
</td>
</tr>
</table>
Related
I'm trying to get the below outputs of this form to limit the outcome to 2 decimal place as it's a currency.
I've tried another suggestion by adding an onchange event but it's not working. Any suggestions?
<form oninput="retVal.value = pavVal.value - (pavVal.value * pavCAT.value),salvage.value=pavVal.value * pavCAT.value, retPrevVal.value = ((pavVal.value *0.8) * (1 - pavCAT.value))">
<table>
<tr>
<td>
<label for="pavVal"><b>PAV:</b></label>
</td>
<td>
<input type="number" id="pavVal">
</td>
</tr>
<tr>
<td>
<label for="pavCAT" ><b>CAT:</b></label>
</td>
<td>
<input type="radio" name="pavCAT" value="0.28" >
<label for="0.28" style="padding-right:30px; width:50%;" >Category N (28%)</label>
<input type="radio" name="pavCAT" value="0.2" >
<label for="0.2" style="padding-right:30px; width:50%;">Category S (20%)</label>
<input type="radio" name="pavCAT" value="0.05" >
<label for="0.05" style="padding-right:30px; width:50%;">Category B (5%)</label>
</td>
</tr>
<tr>
<td>
<label for="retVal"><b>Retention Value:</b></label>
</td>
<td>
<output name="retVal" for="pavVal pavCAT" onchange="setTwoNumberDecimal()"></output>
</td>
</tr>
<tr>
<td>
<label for="retPrevVal"><b>Salvage Value:</b></label>
</td>
<td>
<output name="salvage" for="pavVal pavCAT" onchange="setTwoNumberDecimal()" ></output>
</td>
</tr>
<tr>
<td>
<label for="retPrevVal"><b>If Previous TL:</b></label>
</td>
<td>
<output name="retPrevVal" for="pavVal pavCAT"></output>
</td>
</tr>
</table>
</form>
I've been stuck for hours on a problem that might be actually simple but I can't manage to find the solution.
To be short, I want to know the index (ie the first / the second / the third etc...) of a checkbox when clicking on it.
Here's the jsfiddle showing what's currently working and what is not. I've tried many things but couldn't find the solution I'm looking for.
https://jsfiddle.net/cpydwqk3/2/ <div>(this example applies only for the class "admin" which applies herself on the checkbox "admin").
function usermodif(identifiant) {
alert($('.admin').index(this)); //return -1
}
$("tr").click(function() {
alert($("tr").index(this)); //work but applies on whole line
});
<table>
<caption>Utilisateurs</caption>
<tr>
<th scope="col">pseudo</th>
<th scope="col">points</th>
<th scope="col">points_session</th>
<th scope="col">admin</th>
<th scope="col">banni</th>
</tr>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">Nico</th>
<td><input type="text" name="points" value="21" id="points"></td>
<td><input type="text" name="points_session" value="21" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)" checked> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">anonyme</th>
<td><input type="text" name="points" value="0" id="points"></td>
<td><input type="text" name="points_session" value="0" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin" onclick="usermodif(this.className)"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Thanks in advance guys!
Traverse first tp the closest TR. Than get that closest TR index.
TR has an index amongst TBODY TRs, but not the checkbox.
Also, never use inline JavaScript on* attributes, same as you hopefully don't use inline style attributes. JS and CSS should be in one place only, and that's their respective files or tags.
$(".admin").on("click", function() {
const $par = $(this).closest("tr");
alert($par.index());
});
<table>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
<tr>
<th scope="row">firstguy</th>
<td><input type="text" name="points" value="45" id="points"></td>
<td><input type="text" name="points_session" value="6" id="points_session"></td>
<td><input type="checkbox" class="admin" name="admin"> </td>
<td><input type="checkbox" class="banni" name="banni"> </td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
I want a form where someone choose a unique value of radio buttons vertical and horizontal (you'll see below what i mean). I know tha i can do it with name. In my code below you see same name in vertical.
Here's my code
<tr>
<td style="text-align:left;">tracking_url</td>
<td><input type="radio" name="tracking_url" value="image_url"></td>
<td><input type="radio" name="pr_image" value="tracking_url"></td>
<td><input type="radio" name="availability" value="tracking_url"></td>
<td><input type="radio" name="pr_price" value="tracking_url"></td>
<td><input type="radio" name="pr_final_price" value="tracking_url"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">image_url</td>
<td><input type="radio" name="tracking_url" value="image_url"></td>
<td><input type="radio" name="pr_image" value="image_url"></td>
<td><input type="radio" name="availability" value="image_url"></td>
<td><input type="radio" name="pr_price" value="image_url"></td>
<td><input type="radio" name="pr_final_price" value="image_url"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">availability</td>
<td><input type="radio" name="tracking_url" value="availability"></td>
<td><input type="radio" name="pr_image" value="availability"></td>
<td><input type="radio" name="availability" value="availability"></td>
<td><input type="radio" name="pr_price" value="availability"></td>
<td><input type="radio" name="pr_final_price" value="availability"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">price</td>
<td><input type="radio" name="tracking_url" value="price"></td>
<td><input type="radio" name="pr_image" value="price"></td>
<td><input type="radio" name="availability" value="price"></td>
<td><input type="radio" name="pr_price" value="price"></td>
<td><input type="radio" name="pr_final_price" value="price"></td>
<td></td>
</tr>
<tr>
<td style="text-align:left;">full_price</td>
<td><input type="radio" name="tracking_url" value="full_price"></td>
<td><input type="radio" name="pr_image" value="full_price"></td>
<td><input type="radio" name="availability" value="full_price"></td>
<td><input type="radio" name="pr_price" value="full_price"></td>
<td><input type="radio" name="pr_final_price" value="full_price"></td>
<td></td>
</tr>
This is how i want to use radio buttons
but also someone can use radio buttons like
Is there any way to do it with html or javascript??
var col, el;
$("input[type=radio]").click(function() {
el = $(this);
col = el.data("col");
$("input[data-col=" + col + "]").prop("checked", false);
el.prop("checked", true);
});
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #ccc;
padding: 10px;
}
th:empty {
border: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<table>
<tr>
<th></th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<tr>
<td>Twix</td>
<td><input type="radio" name="row-1" data-col="1"></td>
<td><input type="radio" name="row-1" data-col="2"></td>
<td><input type="radio" name="row-1" data-col="3"></td>
</tr>
<tr>
<td>Snickers</td>
<td><input type="radio" name="row-2" data-col="1"></td>
<td><input type="radio" name="row-2" data-col="2"></td>
<td><input type="radio" name="row-2" data-col="3"></td>
</tr>
<tr>
<td>Butterfingers</td>
<td><input type="radio" name="row-3" data-col="1"></td>
<td><input type="radio" name="row-3" data-col="2"></td>
<td><input type="radio" name="row-3" data-col="3"></td>
</tr>
</table>
https://css-tricks.com/radio-buttons-with-2-way-exclusivity/
The radio buttons within a row need to have the same name="nameValue" value. The value fields can be different for each radio button.
I think it would be better to use checkboxes. And control if other checkboxes could be checked or not using javascript.
Here I implemented in Angular 7 input radio button vertically and horizontal
The Idea behind this is that with radio button we have name property which makes single select either by row or column but for both row and column. I placed the row in name and handled column with javascript whenever button will click.
This is HTML CODE
<section class="editor">
<strong>Editor</strong>
<div>
<label>Add option</label>
<button (click)="addOption()">+</button>
<div *ngFor="let option of options;let i = index">
<span>{{option.title +(i+1)}}</span><button (click)="deleteOption(i)"
*ngIf="options.length>2">X</button>
</div>
</div>
</section>
<hr>
<section>
<strong>Builder</strong>
<div class="builder">
<label>Question title goes here</label><br>
<div *ngFor="let option of options;let row_index = index">
<span>{{option.title +(row_index+1)}}</span>
<span *ngFor="let rank of options;let column_index=index">
<input type="radio" name="{{row_index}}" class="{{column_index}}"
(click)="check(column_index,$event)">
</span>
</div>
</div>
</section>
And this is my .ts file code where I Implemented with Javascript
export class AppComponent {
options = [{
title: "option",
rank: 0,
}, {
title: "option",
rank: 0
}]
constructor() {
}
ngOnInit(): void { }
addOption() {
this.options.push({
title: "option",
rank: 0
})
}
deleteOption(i) {
this.options.splice(i, 1);
}
check(column_index, event) {
Array.from(document.getElementsByClassName(column_index)).map(x=>x['checked']=false);
event.target.checked=true;
}
}
I have table and I would like to put my td's under th tags. Here is my HTML code:
<table>
<tr>
<th>Your Email</th>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
</tr>
<tr>
<th>Job Category</th>
//I need break line here
<td>
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
<tr>
</table>
My output gives me text in <th> tags and then text in <td> tags next to it. I would like to have td content below th. I tried to use <br> tags but looks like that does not work outside of td tags. If anyone know the best and way to fix this please let me know.
You are creating a table and nothing can be put outside the table cells. If you want to have the forms element under the headers, just create one row with headers and one row with forms elements:
<table>
<tr>
<th>Your Email</th>
<th>Job Category</th>
</tr>
<tr>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
<td>
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
<tr>
</table>
Put your table header in its own row:
<table>
<tr>
<th scope="row">Your Email</th>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
</tr>
<tr>
<th colspan="2">Job Category</th>
</tr>
<tr>
//I need break line here
<td colspan="2">
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
</tr>
</table>
Place them in different rows using
<table>
<tr>
<th scope="row">Your Email</th>
<td>
<input type="text" name="userEmail" value="" id="userEmail" required="">
</td>
</tr>
<tr>
<th>Job Category</th>
</tr>//End row
//I need break line here
<tr> //start new row
<td>
<label><input type="radio" name="rd1" value="Programmer" id="rd1">Programmer</label><br>
<label><input type="radio" name="rd2" value="Web Developer" id="rd2">Web Developer</label><br>
</td>
<tr>
</table>
This seems (and should be) simple, but I have no idea why the values for my radio buttons appear empty
<!DOCTYPE html>
<html>
<body>
<form method="post" action="action_page.php">
<table>
<tr>
<th>Intermediate</th><th>Advanced</th><th>No selection</th>
</tr>
<tr>
<td colspan="3">
<label for="td1">1. Bash</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td1" id="td1" value="BashInter"/>
</td>
<td><input type="radio" name="td1" value="BashAdv"/>
</td>
<td><input type="radio" name="td1" value="" /></td>
</tr><tr>
<td colspan="3">
<label for="td1">2. C</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td2" id="td2" value="CInter"/>
</td>
<td><input type="radio" name="td2" value="CAdv"/>
</td>
<td><input type="radio" name="td2" value="" /></td>
</tr><tr>
<td colspan="3">
<label for="td1">3. C++</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td3" id="td3" value="C++Inter"/>
</td>
<td><input type="radio" name="td3" value="C++Adv"/>
</td>
<td><input type="radio" name="td3" value="" /></td>
</tr></table>
<input type="submit" value="Submit">
</form>
<p>If you click "Submit", the form-data will be sent to a page called "action_page.php".</p>
</body>
</html>
If run on W3Schools' (ugh) server, it correctly outputs the input.
e.g.
td1=BashInter&td2=CAdv&td3=
However, my server, for print_r($_POST); returns
[td1] => [td2] => [td3] =>
regardless of what is selected.
Looking at the HTTP headers confirms that nothing is being sent.
All other aspects of the form correctly send their values.
I have tried a number of variants in relation to the values, but nothing has altered the fact that no data actually appears to be sent by the radio buttons.
You didn't specified that your form must be send as a POST one, so used method is defaulted to GET.
Try with
<form action="action_page.php" method="POST">
Last radio button of every set has been kept empty and has black value.
Which means <input type="radio" name="td3" value="" /> needs to be replaced with <input type="radio" name="td3" value="Some value 3" />
So in case you are selecting last radio button from every set then obviously it wont return anything.
Apart from this there is not other problem as long as your HTML code is concerned. If you still face any problem please upload your PHP code as well.
I also suggest to give some some meaningful label to each radio.
Here is the code you may try
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="action_page.php">
<table>
<tr>
<th>Intermediate</th><th>Advanced</th><th>No selection</th>
</tr>
<tr>
<td colspan="3">
<label for="td1">1. Bash</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td1" id="td1" value="BashInter"/> BashInter
</td>
<td><input type="radio" name="td1" value="BashAdv"/> BashAdv
</td>
<td><input type="radio" name="td1" value="Some value 1" /> Some value 1</td>
</tr><tr>
<td colspan="3">
<label for="td1">2. C</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td2" id="td2" value="CInter"/> CInter
</td>
<td><input type="radio" name="td2" value="CAdv"/> CAdv
</td>
<td><input type="radio" name="td2" value="Some value 2" /> Some value 2</td>
</tr><tr>
<td colspan="3">
<label for="td1">3. C++</label>
</td>
</tr>
<tr>
<td>
<input type="radio" name="td3" id="td3" value="C++Inter"/> C++Inter
</td>
<td><input type="radio" name="td3" value="C++Adv"/> C++Adv
</td>
<td><input type="radio" name="td3" value="Some value 3" /> Some value 3</td>
</tr></table>
<input type="submit" value="Submit">
</form>
<p>If you click "Submit", the form-data will be sent to a page called "action_page.php".</p>
</body>
</html>