select all checkbox in header , to select all checkboxes in that column - javascript

I have used the code:
<script type="text/javascript">
$(window).load(function () {
$(document).delegate(".checkall", "click", function(event) {
$(this).closest("table").find(':checkbox').attr('checked', this.checked);
});
});
</script>
This code is working fine when I select/deselect the checkbox in header for the first time. But when I again select the checkbox then this code is not working. The checkboxes are not selected.

You should use .prop() instead of .attr(). And you have to check if an element is already checked.
...
var $checkbox = $(this).closest("table").find(':checkbox');
if ($checkbox.is(':checked')) $checkbox.prop('checked', false);
else $checkbox.prop('checked', true);
...

Related

javascript code for html button: disable dropdown and make div appear based on that dropdown selection

I have a javascript code that dynamically shows a div based upon the answer of a dropdown question in an html form:
//<![CDATA[
$(window).load(function () {
$(document).on('change', '.div-toggle2', function () {
var target = $(this).data('target');
var show = $("option:selected", this).data('show');
$(target).children().addClass('hide');
$(show).removeClass('hide');
});
$(document).ready(function () {
$('.div-toggle2').trigger('change');
});
});//]]>
I additionally have a javascript code that disables this dropdown based upon a button click:
function disable() {
document.getElementById("project_program").disabled = true;
Is there a way to combine the two codes...as in the button would disable the dropdown and show the div dynamically based upon the dropdown answer, all triggered by the button click?
I would think about using .prop(). so: $(""project_program").prop('disabled', true);
in the context of what you're doing it would look something like this:
//<![CDATA[
$(window).load(function () {
$(document).on('change', '.div-toggle2', function () {
var target = $(this).data('target');
var show = $("option:selected", this).data('show');
$(target).children().addClass('hide');
$(show).removeClass('hide');
// disable element on
$(""project_program").prop('disabled', true);
});
$(document).ready(function () {
$('.div-toggle2').trigger('change');
});
});
//]]>

How to select multiple arrays using check box in PHP? Also how to click and select single check box in PHP

In my code the selection of all items is possible
I am using a java script to check all and it properly works.But when check one of them from this table, I can't do it.One value is inserted.How it possible to select separately.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<SCRIPT language="javascript">
$(function () {
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.name').attr('checked', this.checked);
});
// if all checkbox are selected, then check the select all checkbox
// and viceversa
$(".name").click(function () {
if ($(".name").length == $(".name:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
</SCRIPT>
`
try below:
$(function () {
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.name').prop('checked', this.checked);
if (this.checked)
insertText();
});
// if all checkbox are selected, then check the select all checkbox
// and viceversa
$(".name").click(function () {
if ($(".name").length == $(".name:checked").length) {
$("#selectall").prop("checked", "checked");
} else {
$("#selectall").prop("checked", false);
}
insertText();
});
function insertText()
{
var arr = [];
$('.name:checked').each(function() {
arr.push($(this).val());
})
$('.text').val($arr.join(','));
}
});

Deselecting datatables hidden checkbox

I think I have a simple problem here but my jquery is somewhat limited.
I'm using this script to check all the checkboxes in my table rows which are handled by datatables (including the hidden ones from deferred rendering)
It's working for the checking portion, but the unchecking is not working when I want to deselect the boxes. How can I tweak what I have to to uncheck the boxes correctly?
Heres my code:
$('#selectall').on('click', function() { //on click
if(this.checked) { // check select status
var cells = dTable.cells( ).nodes();
$( cells ).find(':checkbox').prop('checked', $(this).is(':checked'));
} else {
var cells = dTable.cells( ).nodes();
$( cells ).find(':checkbox').prop('checked', $(this).is(':not(:checked)'));
}
});
Thanks in advance
I'm partial to this version myself:
$('#selectall').on('click', function() { //on click
var cells = dTable.cells( ).nodes();
$( cells ).find(':checkbox').prop('checked',this.checked);
});
Looks to me like your uncheck code evaluates to true.. which means it would be checking them. Try this instead:
$('#selectall').on('click', function() { //on click
if (this.checked) { // check select status
var cells = dTable.cells( ).nodes();
$( cells ).find(':checkbox').prop('checked',true);
} else {
var cells = dTable.cells( ).nodes();
$( cells ).find(':checkbox').prop('checked',false);
}
});

Uncheck checkbox with jquery?

Is it possible upon page entry to modify this code to uncheck check box id LOTL even if the page is cached?
<script type="text/javascript">
function formReset()
{
document.getElementById("LOTL").reset();
}
</script>
You can use prop method:
$(document).ready(function(){
$('#LOTL').prop('checked', false);
// document.getElementById("LOTL").checked = false
})
This should do the trick:
$(document).ready(function()
{
$('#LOTL').attr('checked', false);
});
Is the LOTL element the checkbox or the form?
If it is the checkbox you might try this:
$( '#LOTL' ).removeAttr( 'checked' );
function formReset(){
document.getElementById("LOTL").checked = false;
}
Try this to uncheck
$(function() {
$('#LOTL').prop('checked', false);
});

JQuery Only Toggle if checkbox gets checked .. possible?

I am using the code below where it toggles between 2 div's.
My problem is that I'm using a checkbox to trigger the toggle.
What I need is that...
If the checkbox is NOT check and the user checks it ... then it toggles but if the checkbox is already checked then do not toggle.
Is this possible at all?
Current code is below:
<script type="text/javascript">
$(function(){
$('.togglelink').click(function(){
$('#one').toggle();
$('#two').toggle();
});
});
</script>
What about adding that ?
$('.togglelink').click(function(){
if($(this).attr('checked')) {
return false;
}else{
$('#one').toggle();
$('#two').toggle();
}
});
<script type="text/javascript">
$(function(){
$('.togglelink').click(function(){ if (!($('.togglelink').attr('checked'))) {
$('#one').toggle();
$('#two').toggle();
}
});
});

Categories

Resources