Check multiple checkboxes with one global funtion - javascript

I am trying to select a list of checkboxes. The inputs are generated dynamically and there is more than one list of checkboxes - so I created a global function by sending in the id of the < table > so that the correct list of checkboxes are checked/unchecked.
I think the looping of the nodes in the nodelist is causing the problem but in my mind, it makes sense, but the checkboxes are not checking and there is no error popping up either.
function checkAll(name) {
var nodeList = $(name).next('input[type="checkbox"]');
var nodes = $(nodeList);
nodes.each(function(node) {
if (!node.disabled) {
node.checked = true;
}
});
}
<table summary="User locations" id="LocationsTable">
<tr>
<th>
<input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
Other locations (multi-store)
<button type="button" onclick="javascript:checkAll('LocationsTable');">All</button>
<button type="button" onclick="javascript:uncheckAll('LocationsTable');">None</button>
</th>
</tr>
<tr id="LocationRow">
<td>
<input type="checkbox" name="Store" id="Store" value="Store"> <span id="StoreName">Store 1</span>
</td>
</tr>
</table>

You can use find here very well.
Consider that your name is an ID, so put in fron of your name the hashtag #name.
Also I would recommend using an event listener. I updated your code to use it therefore I gave your btn an id.
document.getElementById('btn').addEventListener('click', function(){checkAll('LocationsTable')})
function checkAll(name) {
var nodeList = [...$(`#${name}`).find('input[type="checkbox"]')];
nodeList.forEach(function(node) {
if (!node.disabled) {
node.checked = true;
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table summary="User locations" id="LocationsTable">
<tr>
<th>
<input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
Other locations (multi-store)
<button id='btn' type="button" >All</button>
<button type="button" onclick="javascript:uncheckAll('LocationsTable');">None</button>
</th>
</tr>
<tr id="LocationRow">
<td>
<input type="checkbox" name="Store" id="Store" value="Store"> <span id="StoreName">Store 1</span>
<input type="checkbox" name="Store" id="Store" value="Store"> <span id="StoreName">Store 1</span>
<input type="checkbox" name="Store" id="Store" value="Store"> <span id="StoreName">Store 1</span>
</td>
</tr>
</table>

simply use querySelectorAll() :
function checkAll(name)
{
document.querySelectorAll(`#${name} input[type=checkbox]`).forEach(nod=>
{
if (!nod.disabled) nod.checked = true;
});
}

You want to prepend the selector passed to the functions with a # - an id selector. Then all you need in the function is:
$(name).find('input[type="checkbox"]').not(':disabled').prop('checked',true);
DEMO
function checkAll(name) {
$(name).find('input[type="checkbox"]').not(':disabled').prop('checked',true);
}
function uncheckAll(name) {
$(name).find('input[type="checkbox"]').not(':disabled').prop('checked',false);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table summary="User locations" id="LocationsTable">
<tr>
<th>
<input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
Other locations (multi-store)
<button type="button" onclick="javascript:checkAll('#LocationsTable');">All</button>
<button type="button" onclick="javascript:uncheckAll('#LocationsTable');">None</button>
</th>
</tr>
<tr id="LocationRow">
<td>
<input type="checkbox" name="Store" id="Store1" value="Store"> <span id="StoreName">Store 1</span>
<input type="checkbox" name="Store" id="Store2" value="Store"> <span id="StoreName">Store 2</span>
<input type="checkbox" name="Store" id="Store3" value="Store"> <span id="StoreName">Store 3</span>
<input type="checkbox" name="Store" id="Store4" value="Store" disabled> <span id="StoreName">Store 4</span>
</td>
</tr>
</table>
OPTION 2
However, I would not advise use of inline JS. Separate your JS, CSS, and HTML as in the following demo:
$('button.all').on('click', function() {
//this refers to button.all
$(this)
//go up the tree to select the table
.closest('table')
//go down to the checkboxes and check them
.find('input[type="checkbox"]').not(':disabled').prop('checked',true);
});
DEMO
$('button.all').on('click', function() {
//this refers to button.all
$(this)
//go up the tree to select the table
.closest('table')
//go down to the checkboxes and check them
.find('input[type="checkbox"]').not(':disabled').prop('checked',true);
});
$('button.none').on('click', function() {
//this refers to button.none
$(this)
//select tabl
.closest('table')
//select checkbox and uncheck
.find('input[type="checkbox"]').not(':disabled').prop('checked',false);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table summary="User locations" id="LocationsTable">
<tr>
<th>
<input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
Other locations (multi-store)
<button type="button" class="all">All</button>
<button type="button" class="none">None</button>
</th>
</tr>
<tr id="LocationRow">
<td>
<input type="checkbox" name="Store" id="Store1" value="Store"> <span id="StoreName">Store 1</span>
<input type="checkbox" name="Store" id="Store2" value="Store"> <span id="StoreName">Store 2</span>
<input type="checkbox" name="Store" id="Store3" value="Store"> <span id="StoreName">Store 3</span>
<input type="checkbox" name="Store" id="Store4" value="Store" disabled> <span id="StoreName">Store 4</span>
</td>
</tr>
</table>
OPTION 3
Alternatively, you can use one button to toggle all the checkboxes.
$('button.toggle').on('click', function() {
//this refers to button.all
$(this)
//go up the tree to select the table
.closest('table')
//go down to the checkboxes and check them
.find('input[type="checkbox"]').not(':disabled').prop('checked',function() {return !this.checked});
});
DEMO
$('button.toggle').on('click', function() {
//this refers to button.all
$(this)
//go up the tree to select the table
.closest('table')
//go down to the checkboxes and check them
.find('input[type="checkbox"]').not(':disabled').prop('checked',function() {return !this.checked});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table summary="User locations" id="LocationsTable">
<tr>
<th>
<input type='hidden' name='StoreSelector' id='StoreSelector' value='true'>
Other locations (multi-store)
<button type="button" class="toggle">All</button>
<!--button type="button" class="none">None</button-->
</th>
</tr>
<tr id="LocationRow">
<td>
<input type="checkbox" name="Store" id="Store1" value="Store"> <span id="StoreName">Store 1</span>
<input type="checkbox" name="Store" id="Store2" value="Store"> <span id="StoreName">Store 2</span>
<input type="checkbox" name="Store" id="Store3" value="Store"> <span id="StoreName">Store 3</span>
<input type="checkbox" name="Store" id="Store4" value="Store" disabled> <span id="StoreName">Store 4</span>
</td>
</tr>
</table>
NOTE
Please note that in each option no reference is made to the id of the particular table in question as the event handler keeps the actions within the particular table whose button was clicked.

Related

Issue in functioning of radio input in dynamically adding row through jQuery

A new row is dynamically creating on clicking "add Row" button through function in jQuery.
But when I select radio input in new row then selected value of previous radio input is removing.
Please run and see the codes as below:
enter image description here
function add_row(table_row_count)
{
$("#add-row_"+table_row_count).hide();
$("#delete-row_"+table_row_count).hide();
var table_row_count = parseInt(table_row_count);
table_row_count += 1;
var markup =
'<tr id="tbl_row_'+table_row_count+'" >'+
'<td><label>'+table_row_count+'</label></td>'+
'<td>'+
'<label>Value in Range? :</label>'+
'<input type="radio" name="option" id="option_1_row_'+table_row_count+'" value="1" > YES'+
'<input type="radio" name="option" id="option_2_row_'+table_row_count+'" value="2" > NO'+
'</td>'+
'<td id="capacity_from_row_'+table_row_count+'" >'+
'<label>Range From :</label><br>'+
'<input type="text" name="capacity_from[]" id="capacity_from_'+table_row_count+'" />'+
'</td>'+
'<td>'+
'<label id="lbl_capacity_range_'+table_row_count+'" >Range Upto :</label><br>'+
'<input type="text" name="capacity_upto[]" id="capacity_upto_'+table_row_count+'" />'+
'</td>'+
'<td class="align-middle"></i> Add Row</td>'+
'<td class="align-middle"></i> Delete Row</td>'+
'</tr>';
var table = $("#tbl_details tbody");
table.append(markup);
}
function delete_row(table_row_count)
{
var r = confirm("Are you sure to delete row");
if(r == false){
return;
}
var table_row_count = parseInt(table_row_count);
var previous_row = table_row_count - 1;
$("#add-row_"+previous_row).show();
if(previous_row != 1){
$("#delete-row_"+previous_row).show();
}
$("#tbl_row_"+table_row_count).remove();
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="add.js"></script>
</head>
<body>
<h3>Enter Details</h3>
<table class="table table-striped" id="tbl_details">
<tbody>
<tr id="row_1">
<td>1.</td>
<td>
<label>Value in Range? :</label>
<input type="radio" name="option" id="option_1_row_1" value="1" checked="checked" > YES
<input type="radio" name="option" id="option_2_row_1" value="2" > NO
</td>
<td id="capacity_from_row_1" >
<label>Range From :</label><br>
<input type="text" name="capacity_from[]" id="capacity_from_1" />
</td>
<td>
<label id="lbl_capacity_range_1" >Range Upto :</label><br>
<input type="text" name="capacity_upto[]" id="capacity_upto_1" />
</td>
<td class="align-middle"></i> Add Row</td>
<td class="align-middle"></td>
</tr>
</tbody>
</table>
</body>
</html>
How to solve the issue. kindly help me.
Thanks in advance.
Different radio button groups are created when the name attribute of the radio buttons is different. Also, in the solution below, I made it easier to express dynamic content by using template literals.
Note the following statement in the dynamic element written into the markup variable in the add_row() method:
<input type="radio" name="option${counter}" id="option_1_row_${table_row_count}" value="1"> YES
<input type="radio" name="option${counter}" id="option_2_row_${table_row_count}" value="2"> NO
/* This variable will be used to create new radio button groups. */
let counter = 0;
function add_row(table_row_count) {
$("#add-row_"+table_row_count).hide();
$("#delete-row_"+table_row_count).hide();
var table_row_count = parseInt(table_row_count);
table_row_count += 1;
/* With each iteration, the content of the counter variable is incremented. */
++counter;
var markup =
`<tr id="tbl_row_${table_row_count}">
<td>
<label>${table_row_count}</label>
</td>
<td>
<label>Value in Range? :</label>
<!-- Note the name="option${counter}" statement so that the radio buttons have different groups. -->
<input type="radio" name="option${counter}" id="option_1_row_${table_row_count}" value="1"> YES
<input type="radio" name="option${counter}" id="option_2_row_${table_row_count}" value="2"> NO
</td>
<td id="capacity_from_row_${table_row_count}">
<label>Range From :</label><br>
<input type="text" name="capacity_from[]" id="capacity_from_${table_row_count}"/>
</td>
<td>
<label id="lbl_capacity_range_${table_row_count}">Range Upto :</label><br>
<input type="text" name="capacity_upto[]" id="capacity_upto_${table_row_count}"/>
</td>
<td class="align-middle">
<a href="javascript:void(0)" class="text-success add-row" id="add-row_${table_row_count}" onClick="add_row('${table_row_count}');" >
<i class="fa fa-plus fa-lg text-success" aria-hidden="true"></i> Add Row
</a>
</td>
<td class="align-middle">
<a href="javascript:void(0)" class="text-danger delete-row" id="delete-row_${table_row_count}" onClick="delete_row('${table_row_count}');" >
<i class="fa fa-trash fa-lg text-danger" aria-hidden="true"></i> Delete Row
</a>
</td>
</tr>`
var table = $("#tbl_details tbody");
table.append(markup);
}
function delete_row(table_row_count) {
var r = confirm("Are you sure to delete row");
if(r == false){
return;
}
var table_row_count = parseInt(table_row_count);
var previous_row = table_row_count - 1;
$("#add-row_"+previous_row).show();
if(previous_row != 1){
$("#delete-row_"+previous_row).show();
}
$("#tbl_row_"+table_row_count).remove();
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="add.js"></script>
</head>
<body>
<h3>Enter Details</h3>
<table class="table table-striped" id="tbl_details">
<tbody>
<tr id="row_1">
<td>1.</td>
<td>
<label>Value in Range? :</label>
<input type="radio" name="option" id="option_1_row_1" value="1" checked="checked" > YES
<input type="radio" name="option" id="option_2_row_1" value="2" > NO
</td>
<td id="capacity_from_row_1" >
<label>Range From :</label><br>
<input type="text" name="capacity_from[]" id="capacity_from_1" />
</td>
<td>
<label id="lbl_capacity_range_1" >Range Upto :</label><br>
<input type="text" name="capacity_upto[]" id="capacity_upto_1" />
</td>
<td class="align-middle"></i> Add Row</td>
<td class="align-middle"></td>
</tr>
</tbody>
</table>
</body>
</html>
This is happening because you are using the same name for radio button. And it is default behavior of radio that if you are using the same name for radio button then only one can be selected at time.
For Example
1.) If you have two radios with same name
Only one value can be selected at a time.
2.) Now after adding another row your code will look like
<input name="option" type="radio" value="true"/>
<input name="option" type="radio" value="false"/
<input name="option" type="radio" value="true"/>
<input name="option" type="radio" value="false"/>
name is same for all radio buttons so only one will be selected at a time.
Fix:
In order to fix this issue while adding new row append row number in front of name. That will fix the issue like you did for id attribute.
'<td>'+
'<label>Value in Range? :</label>'+
'<input type="radio" name="option_'+table_row_count+'" id="option_1_row_'+table_row_count+'" value="1" > YES'+
'<input type="radio" name="option_'+table_row_count+'" id="option_2_row_'+table_row_count+'" value="2" > NO'+
'</td>'+
Fixing this will fix the issue.
*** Change dynamic radio input name. here, your code create all dynamic radio input with the same name. ***

Sort Checkboxes by Label

So I have these checkboxes with crazy IDs because reasons that I can't change. For example:
<td>
<span title=BBB class="ms-RadioText">
<input id = Checkbox1 type=checkbox/>
<label for="Checkbox1">BBB</label>
</span>
<span title=AAA class="ms-RadioText">
<input id = Checkbox2 type=checkbox/>
<label for="Checkbox2">AAA</label>
</span>
</td>
And, of course, they appear non-alphabetically. How do I sort this alphabetically using native JavaScript, JQuery, or CSS, as I am not allowed to alter the order of the checkboxes in HTML?
jQuery sort and read the title attribute
$('td').each(function() {
var td = $(this)
td.find('span').sort(function(a, b) {
return a.title.localeCompare(b.title)
}).appendTo(td)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>
<span title=BBB class="ms-RadioText">
<input id="Checkbox1" type="checkbox"/>
<label for="Checkbox1">BBB</label>
</span>
<span title=AAA class="ms-RadioText">
<input id="Checkbox2" type="checkbox"/>
<label for="Checkbox2">AAA</label>
</span>
</td>
</tr>
<tr>
<td>
<span title=FFF class="ms-RadioText">
<input id="Checkbox1b" type="checkbox"/>
<label for="Checkbox1b">FFF</label>
</span>
<span title=ZZZ class="ms-RadioText">
<input id="Checkbox2b" type="checkbox"/>
<label for="Checkbox2b">ZZZ</label>
</span>
<span title=EEE class="ms-RadioText">
<input id="Checkbox3b" type="checkbox"/>
<label for="Checkbox3b">EEE</label>
</span>
</td>
</tr>
</table>
Without jQuery
document.querySelectorAll('td').forEach(function(td) {
const spans = td.querySelectorAll('span')
const ordered = Array.from(spans).sort( function (a,b) {
return a.title.localeCompare(b.title)
})
var frag = ordered.reduce(function (frag, span){
frag.appendChild(span)
return frag
}, document.createDocumentFragment())
td.appendChild(frag)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>
<span title=BBB class="ms-RadioText">
<input id="Checkbox1" type="checkbox"/>
<label for="Checkbox1">BBB</label>
</span>
<span title=AAA class="ms-RadioText">
<input id="Checkbox2" type="checkbox"/>
<label for="Checkbox2">AAA</label>
</span>
</td>
</tr>
<tr>
<td>
<span title=FFF class="ms-RadioText">
<input id="Checkbox1b" type="checkbox"/>
<label for="Checkbox1b">FFF</label>
</span>
<span title=ZZZ class="ms-RadioText">
<input id="Checkbox2b" type="checkbox"/>
<label for="Checkbox2b">ZZZ</label>
</span>
<span title=EEE class="ms-RadioText">
<input id="Checkbox3b" type="checkbox"/>
<label for="Checkbox3b">EEE</label>
</span>
</td>
</tr>
</table>
With vanilla JavaScript you can sort your checkboxes with the following function.
function sortCheckboxesByLabel(parentElement) {
var children = Array.prototype.slice.call(parentElement.children), // Convert to true array.
fragment = document.createDocumentFragment();
// Sort alphabetically.
children.sort((item1, item2) => item1.querySelector('label').innerText > item2.querySelector('label').innerText ? 1 : -1);
// Add to fragment.
for(var i = 0; i < children.length; i++) {
fragment.appendChild(children[i]);
}
// Append back to document.
parentElement.appendChild(fragment);
}
It takes their parent element as it’s only parameter. It converts the parent elements children to an array and then sorts the array alphabetically. It then appends those items into a document fragment and finally back to the parent element.
You could take advantage of the DOM API to grab the span elements from the parent, re-order them, and append a document fragment back to the original parent element. Something like below:
const orderEls = () => {
//Get an array of the elements we want to sort
const spanEls = Array.from(document.querySelectorAll('span'));
//Generate a doc fragment containing the ordered els
const frag = spanEls
.sort((a, b) => a.title > b.title ? 1 : -1)
.reduce((accum, spanEl) => {
accum.appendChild(spanEl);
return accum;
}, document.createDocumentFragment());
//Append the ordered els to the parent
document.querySelector('td').appendChild(frag)
};
orderEls();
<table>
<tr>
<td>
<span title="BBB" class="ms-RadioText">
<input id="Checkbox1" type="checkbox" />
<label for="Checkbox1">BBB</label>
</span>
<span title="AAA" class="ms-RadioText">
<input id="Checkbox2" type="checkbox"/>
<label for="Checkbox2">AAA</label>
</span>
</td>
</tr>
</table>

document.getElementById('').display=block not working

In the following document, when someone clicks radios (which are in a table) function StOptnClkd is called. It accomplishes everything but the most important thing. There are two elements that need to switch from being diplayed to not displayed- namely, - AmxDisc and stOptnsTbl. These elements are displayed based on the selection on the radio buttons and check boxes.Please help me. I am a newbie trying to learn to code
I have attached the following code
<script type="text/javascript">
function StOptnClkd(clicked_id)
{
//alert(clicked_id)
var strvid = clicked_id;
var StVal = document.getElementById(strvid).value;
document.getElementById("STtypCptrd").checked="Checked";
document.getElementById("cntctpe").innerHTML=StVal;
document.getElementById('Autonote').value = document.getElementById('Autonote').value+"Spke to: " + StVal; + ", ";
document.getElementById("AmxDisc").display="Block" // this doesnt show
document.getElementById("stOptnsTbl").display="None" // this doesnt get hidden
}
</script>
<table id="stOptnsTbl" border cellspacing="50px" style="text-align: center; border: 1px solid black; border-collapse: collapse;float: left; display: none">
<tr> <!--All Party Options-->
<td width="110px" id ="CMOptn" class="stOptions">
<label class="container1" style="float:Left"><Strong> CM</Strong>
<input id= "optstCM" type="radio" value ="CM" Name="stPrtysel" onclick="StOptnClkd(this.id)">
<span class="checkmark1"></span>
</label>
</td>
<td width="110px" id ="UTPOptn" class="stOptions">
<label id="lblUPT" class="container1" style="float:Left"><Strong> UTP</Strong>
<input id= "optUTP" type="radio" value ="UTP" Name="stPrtysel" checked="checked" onclick="StOptnClkd(this.id)" >
<span class="checkmark1"></span>
</label>
</td>
<td width="110px" id ="SPOptn" class="stOptions">
<label id="lblSP" class="container1" style="float:Left;"><Strong> SP </Strong>
<input id= "optSP" type="radio" value ="SP" Name="stPrtysel" onclick="StOptnClkd(this.id)" >
<span class="checkmark1"></span>
</label>
</td>
<td width="110px" id ="SUPOptn" class="stOptions">
<label id="lblSUP" class="container1" style="float:Left;"><Strong> SUP </Strong>
<input id= "optSUP" type="radio" value ="SUP" Name="stPrtysel"onclick="StOptnClkd(this.id)" >
<span class="checkmark1"></span>
</label>
</td>
<td width="110px" id ="ATPOptn" class="stOptions">
<label id="lblATP" class="container1" style="float:Left;"><Strong> ATP </Strong>
<input id= "optATP" type="radio" value ="ATP" Name="stPrtysel"onclick="StOptnClkd(this.id)" >
<span class="checkmark1"></span>
</label>
</td>
<td width="110px" id ="ATTOptn" class="stOptions">
<label id="lblATT" class="container1" style="float:Left;"><Strong> ATT </Strong>
<input id= "optATT" type="radio" value ="AM" Name="stPrtysel" onclick="StOptnClkd(this.id)">
<span class="checkmark1"></span>
</label>
</td>
<td width="110px" id ="AMOptn" class="stOptions">
<label id="lblAM" class="container1" style="float:Left;"><Strong> AM </Strong>
<input id= "optAM" type="radio" value ="AM" Name="stPrtysel" onclick="StOptnClkd(this.id)" >
<span class="checkmark1"></span>
</label>
</td>
</span>
</span>
</tr>
</table>
<span width="100%" id="AmxDisc" style="display:none;">
has referred your account to for resolution.
EDIT: IT IS THERE, coudnt post all the code here, If you guys need the whole code tell me, I can mail it. or find a way to put it here
Edit2: I did the style.display change. It still doesnt do it. IN console, it shows error saying unexpected identfier at that line
document.getElementById("AmxDisc").style.display="block"

changing button to submit button in my javascript

I had this major failure in my codes, I forgot that buttons cant be isset() in PHP only submit buttons.
I used a next and prev buttons in my form, so they can next and prev the checked radio button.
but I need validation of form before going to the next radio button, when I tried converting the button to submit button it glitches and doesn't go to the next radio button.
help me convert these button codes to submit and arrange my javascript, been trying for the whole day I think I'm forgetting something again...
HTML
<form action="reservation_next_sample.php" method="POST">
<input type="radio" name="next[]" value="1" checked="checked">
<input type="radio" name="next[]" value="2" />
<div id="next1" class="desc">
<div class="div-details">
<h4>Event's Detail</h4>
<table>
<tr>
<td>
Street
</td>
<td>
<input type="text" name="event_street">
</td>
</tr>
<tr>
<td>
Barangay
</td>
<td>
<input type="text" name="event_brgy">
</td>
</tr>
<tr>
<td>
Town/City
</td>
<td>
<input type="text" name="event_town_city">
</td>
</tr>
<tr>
<td>
Province
</td>
<td>
<input type="text" name="event_province">
</td>
</tr>
</table>
<br>
<button type="button" onclick="dayNavigation('next');" data-role="none" class="slick-prev" aria-label="Next" tabindex="0" role="button">Next</button>
</div>
<div id="next2" class="desc" style="display: none;">
<p> inside of next 2 </p>
<button type="button" onclick="dayNavigation('prev');" data-role="none" class="slick-next" aria-label="Previous" tabindex="0" role="button">Previous</button>
<button type="button" onclick="dayNavigation('next');" data-role="none" class="slick-prev" aria-label="Next" tabindex="0" role="button">Next</button>
</div>
</form>
JAVASCRIPT
<script>
$(document).ready(function() {
$("input[name$='next[]']").click(function() {
var test = $(this).val();
$("div.desc").hide();
$("#next" + test).show();
});
});
//this is in the bla bla next and previous -->
var index = 0;
dayNavigation = function(direction) {
var curr = $('input[name="next[]"]:checked');
if (direction == 'next') {
curr.next().attr("checked", "checked");
curr.next().click();
} else {
curr.prev().attr("checked", "checked");
curr.prev().click();
}
};
</script>
You can submit your form using jQuery example is given below. Don't change button to submit
$("button[type=button]").click(function(){
this.closest("form").submit();
})
Instead of selecting button like $("button[type=button]") give button id and select particular button $("#ButtonIdWillGoesHere")

When I pass to another radio button both buton's values come

I want to do two radio buttons. Their names will be car and home. If I click car, an input box about the car section will be shown and if I click home, an input box about the home will be shown.
I have found an example about this and I have a problem about it. The codes are below.
<body>
<script type="text/javascript">
function toggleMe(obj, a)
{
var e=document.getElementById(a);
if(!e)return true;
e.style.display="block"
return true;
}
function toggleMe2(obj, a)
{
var e=document.getElementById(a);
if(!e)return true;
e.style.display="none"
return true;
}
</script>
<form name="theForm">
Type<br>
<input type="radio" name="married" value="yes" onclick="return toggleMe(this, 'Car')"> Car
<input type="radio" name="married" value="yes" onclick="return toggleMe(this, 'Home')"> Home<br>
<div id="Car" style="display: none; margin-left: 20px;">
<table>
<tr>
<td>Car InputBox</td>
<td style="text-align: right;"><input name="name" type="text"></td>
</tr>
</table>
</div>
<div id="Home" style="display: none; margin-left: 20px;">
<table>
<tr>
<td>Home InputBox:</td>
<td style="text-align: right;"><input name="name" type="text"></td>
</tr>
</table>
</div>
</form>
</body>
Output of these codes
If I click the car first, the input box of the car section is shown. If I click the home first, the input box of the home section is shown. After I click the car or home first (doesn't matter) if I click another one, both input boxes are shown.
How can I solve this problem? When I click home, the home input box should be shown and when I click car, the car input box should be shown as well. Not both of them.
HTML
<div>
<input type="radio" name="option" value="car" onclick="toggleInput(this)" /> Car
<input type="radio" name="option" value="home" onclick="toggleInput(this)" /> Home
</div>
<div id="car-input-group" class="hide">
Car InputBox
<input name="name" type="text" />
</div>
<div id="home-input-group" class="hide">
Home InputBox
<input name="name" type="text" />
</div>
Javascript
function toggleInput(obj){
var a =document.getElementsByClassName("hide");
for(var i=0; i<a.length; i++){
a[i].style.display = "none";
}
document.getElementById(obj.value + "-input-group").style.display = "block"
}
css
.hide{
display: none
}
Working example: http://plnkr.co/edit/wPdTldCXG0LTJWEtzIxE?p=preview
What about this? Using jquery it's easier. I made it universal, so you could add more radio buttons (using myradio and myinput css classes) without touching the JS.
$( "input.myradio" ).click(function() {
$( "div.myinput").hide();
$( "div#"+$(this).attr('id')).toggle( "slow", function() {
// Animation complete.
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="theForm">
Type<br>
<input id="Car" type="radio" name="married" class="myradio" value="yes" />Car
<input id="Home" type="radio" name="married" class="myradio" value="yes" />Home<br>
<div id="Car" class="myinput" style="display: none; margin-left: 20px;">
<table>
<tr>
<td>Car InputBox</td>
<td style="text-align: right;"><input name="name" type="text"></td>
</tr>
</table>
</div>
<div id="Home" class="myinput" style="display: none; margin-left: 20px;">
<table>
<tr>
<td>Home InputBox:</td>
<td style="text-align: right;"><input name="name" type="text"></td>
</tr>
</table>
</div>
</form>
JSFiddle

Categories

Resources