I want to trigger a programmatically checked checkbox. eg:
$(function(){
//create a trigger that if a checkbox changes, eg is clicked/change show an alert
$("#idCheckbox").on("change", function(){
if($(this).is(':checked')){
alert("Is checked!");
}
});
//onload put it checked
$("#idCheckbox").attr("checked", "checked");
});
The 'problem' is that if I check on load the checkbox by javascript the on() method is not triggered, and the alert is not shown.
The code above is an example, in fact the script which check the checkbox is not mine and I can't modify it.
I am using jQuery 1.8.0
Any suggestion?
Thanks in advance.
Rather than setting the attribute to checked you can call click() on the checkbox to trigger the the event you had bound previously
$(function(){
var $checkbox = $("#idCheckbox");
$checkbox.on("change", function(){
if(this.checked){
alert("Is checked!");
}
});
$checkbox.click();
});
example: http://jsfiddle.net/hunter/wpLb2/
You can move your inline defined function to global scope and call it:
function checkBoxClick(){
if($(this).is(':checked')){
alert("Is checked!");
}
}
$(function(){
//create a trigger that if a checkbox changes, eg is clicked/change show an alert
$("#idCheckbox").on("change", checkBoxClick);
// Trigger your click function
checkBoxClick();
//onload put it checked
$("#idCheckbox").attr("checked", "checked");
});
When a checkbox is checked programmatically using javascript, onchange event does not get fired automatically. So the script that marks the checkbox as checked should call onchange event.
Related
guys. I have a problem that i can't resolve. I need to switch the behavior using ".on/.off" methods that will be working when i set or remove the tick from the checkbox. How can i do that?
For example: I have a button that will be zooming in/change color/moving by pressing on it. But when i set the tick on one of a checkboxes, i need to disable this event from the button using ".off" method.
I tried like
$(".button").on('click', function(){
$(this).toggleClass('active');
});
$(".button2").on('click', function(){
$(this).toggleClass('active2');
});
$(".button3").on('click', function(){
$(this).toggleClass('active3');
});
$(".button4").on('click', function(){
$(this).toggleClass('active4');
});
var check = $("#check");
check.change(function(){
if ($(this).is(':checked'))
{
$('.button').off('click');
}
});
but for some reason it will disable my event until i refresh the page, even if i remove the tick from one of my checkboxes
Thanks in advance.
This is happening because when you tick the checkbox, your code will remove the click listener from the .button element and you're not setting it back when the checkbox is unchecked.
So, if you want the click listener on the .button element to fire again when the checkbox is unchecked, you can do any one of the two things as mentioned below.
First, you can add an else block after the if block in the change listener of checkbox. Following code will work for this approach:
var check = $("#check");
check.change(function(){
if ($(this).is(':checked')) {
// This will remove the click listener from the button if checkbox is checked
$('.button').off('click');
}
else {
// This will again add the click listener to the button if checkbox is not checked
$(".button").on('click', function(){
$(this).toggleClass('active');
});
}
});
But I will not recommend the above approach because it will unnecessarily increase the size of code.
Here's the second and a better approach. Inside the click event handler function, you can add an if statement to check if the checkbox is ticked. Below is the code for the same:
$(".button").on('click', function(){
if (! $("#check").is(':checked')) {
// This will only be fired if checkbox is not checked
$(this).toggleClass('active')
}
});
You can add this if to any other block according to your requirement.
The main advantages of using the above code are:
Code will not be increased as compared to 1st approach
You don't need a change listener for the checkbox (Yeah! Just remove it!)
checkbox can not checked or uncheck by directly click on checkbox ?
i was create click on div for toggle checked/unchecked checkbox. this function was best.
But when i tried to click on checkbox directly, i can not check or uncheck checkbox.
How can i do that ?
https://jsfiddle.net/jddwxtst/
<script>
function onclick_cover_checkbox_fn(){
var main_checkbox_checked_uncheck_var = document.getElementById("main_checkbox_checked_uncheck");
var main_checkbox_checked_uncheck_var_checked_status = main_checkbox_checked_uncheck_var.checked;
if(main_checkbox_checked_uncheck_var_checked_status != true)
{
document.getElementById("main_checkbox_checked_uncheck").checked = true;
checked_all_or_uncheck_all_fn();
}
else
{
document.getElementById("main_checkbox_checked_uncheck").checked = false;
checked_all_or_uncheck_all_fn();
}
}
</script>
<script>
function checked_all_or_uncheck_all_fn(){
alert("5555");
}
</script>
Here is a example in jquery:
Bind a change handler, then just uncheck all of the checkboxes, apart from the one checked:
$('input.example').on('change', function() {
$('input.example').not(this).prop('checked', false);
});
Here's a fiddle
here is the updated working example https://jsfiddle.net/jddwxtst/2/
problem is when you directly click on the checkbox it first run checked_all_or_uncheck_all_fn() method but then it execute onclick_cover_checkbox_fn() method too (you don't need) because div also receive click event.so if you check then method onclick_cover_checkbox_fn() method will uncheck it .to avoid this you shroud stop event propagating in checked_all_or_uncheck_all_fn() method.
function checked_all_or_uncheck_all_fn(e){
alert("5555");
// stop event propagating
}
for cross browser stop propagating read this http://javascript.info/tutorial/bubbling-and-capturing
Is there any event, to append to the following listener, that will tell me if a radio button is already clicked?
var budgetType;
$(document).on('change', 'input[name="import_export"]', function() {
console.log($(this))
budgetType = $(this).val();
});
My problem is that, on page reload, if the radio box is already checked, the change listener won't work, and I didn't find any listener that will help me with that issue.
Any ideas?
Update
I'm aware that I could write an if/else syntax and see which is checked, but I was wondering if there is any other listener that I could append to change one, and avoid writing that extra syntax.
http://jsfiddle.net/S93XB/1/
$('input[type=radio]').is(':checked')
Try this code here: http://jsfiddle.net/afzaal_ahmad_zeeshan/rU2Fc/
or try this one
$('input[type=radio]').prop('checked', true);
Try this code here: http://jsfiddle.net/afzaal_ahmad_zeeshan/rU2Fc/1/
It will work, you can execute this onload to get the first value and use it in an if else block.
Trigger the change event on load:
var budgetType;
$(document).on('change', 'input[name="import_export"]', function() {
var isChecked = $(this).is(":checked");
console.log($(this).val()+" "+isChecked);
budgetType = $(this).val();
});
$('input[name="import_export"]:checked').trigger('change');
http://jsfiddle.net/eLGaB/
Try this :
$('input[type=radio]').is(':checked')
Or
$('input[type=radio]').prop('checked') === true;
Or
$('input[type=radio]').attr('checked') === 'checked';
I'm using checkboxes to toggle the enabled and disabled state of some multi-lists on a registration form. The checkbox is labeled with the category, and the multi-list contains the items that belong to that category.
I'm using jQuery 1.7.2.
$('#sch_cat_hockeyschools').toggle(function(ev) {
ev.stopPropagation();
$("#type_select_hockeyschools").prop("disabled", false);
$("#type_select_hockeyschools").removeProp("disabled", "disabled");
$("#sch_cat_hockeyschools").prop("checked", true);
$("#sch_cat_hockeyschools").prop("checked", "checked");
}, function(ev) {
ev.stopPropagation();
$("#type_select_hockeyschools option:selected").removeAttr("selected");
$("#type_select_hockeyschools").prop("disabled", true);
$("#type_select_hockeyschools").prop("disabled", "disabled");
$("#sch_cat_hockeyschools").prop("checked", false);
$("#sch_cat_hockeyschools").removeProp("checked");
});
Sample of corresponding checkbox HTML:
<input class="catmark" type="checkbox" name="sch_categories[]" id="sch_cat_hockeyschools" value="1" />General Hockey Schools
<input class="catmark" type="checkbox" name="sch_categories[]" id="sch_cat_springhockey" value="2" />Spring Hockey
The problem is that the upon clicking the checkbox, the checkbox does not become ticked or checked; it immediately returns to an unchecked state, which I thought the stopPropagation() function would help with. Apparently not. The multi-lists get enabled and disabled as expected, but the checkbox doesn't get ticked.
The result of this problem is that when the form is submitted, the array containing the selected categories is empty; thus, because at least one checked category is a required field in the form, the PHP script that processes the form throws one of my errors which tells me a required field was left blank.
Any ideas on how to make sure that the checkbox actually gets checked, and by extension, POSTS actual data to the processing script?
Thanks guys.
The problem is the use of toggle -- per the documentation:
The implementation also calls .preventDefault() on the event, so links
will not be followed and buttons will not be clicked if .toggle() has
been called on the element.
toggle itself is calling preventDefault, which is stopping the default behavior of the event, checking/unchecking the box.
Rather than toggle, use bind or on (see edit note below) to add a listener for the change event, in which you can examine the state:
$('#sch_cat_hockeyschools').on('change', function () {
if (this.checked) {
// do stuff for a checked box
console.log('check is on');
} else {
// do stuff for an unchecked box
console.log('check is off');
}
});
Try it out at jsFiddle.
EDIT
Please note, this code shows use of the on method, whereas the jsFiddle example uses bind. As pointed out by Sam Sehnert, on is the preferred method for attaching events with > jQuery 1.7. If you are using an older version of jQuery, use bind as in the jsFiddle example.
Documentation
jQuery.toggle
jQuery.bind
jQuery.on
I have a checkbox column in gridview and getting its Click event. But I just has relised I do not need Click event rather I need to know the events if checkbox is checked or unchecked on Client side. But these events are not available.
Please guide how to handle this.
use delegate on the column by setting a css class to the checkboxs.
then listen to the click event of these checkboxes and check the .checked property of the actual dom element
you are probably looking to check that the checkbox input is(":checked")
Check out my example on jsfiddle: http://jsfiddle.net/RWCZK/
$("#checked").click(function()
{
if($(this).is(":checked"))
{
$("#show").hide();
}
else
{
$("#show").show();
}
});
You can still use your click event. Inside of the function use:
$('#mycheckbox').click(function()
{
if($(this).prop('checked'))
{
//Do something here
}
else
{
//Do something else here
}
});