I created kind of custom checkbox's plugin for my own project.
So I wanted to trigger <input type="checkbox"> manually clicking on another button like this:
$("a#triggerButton").click(function(){
$("input[type='checkbox']")
.prop("checked", true)
.triggerHandler("change");
});
Here is a typical example of what I'm doing: http://jsfiddle.net/fstqvq8k/3/
It seems that the event isn't triggered. I used a lot of methods but not seems to work too:
$("input[type='checkbox']").change();
$(document).on("change", "input[type='checkbox']", function(){});
Since you dynamically create your button:
$("#container").on("click", "#triggerButton", function() {
$("#customCheckbox")
.prop("checked", true)
.trigger("change");
});
http://jsfiddle.net/fstqvq8k/4/
Also from the Docs read:
The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission).
You can also trigger the checkbox event like..
$("input[type='checkbox']").click();
Related
I have following radio button in my page
<input type="radio" name="launch_selection" class="linkToSectionSurvey expandSectionSurvey" id="linkTosectionSurvey1" rel="Surveysection1">
<input type="radio" name="launch_selection" id="all_active_members" class="linkToSectionSurvey expandSectionSurvey">
And this the code in my js file, which I am using for radio button click event.
jQuery('input[type=radio].expandSectionSurvey').live('click',function(e){
});
My requirement is to check the class clicked.
Jquery function is getting called on radio button clicked.
Only issue is its not showing "checked" even after it is clicked.
Its an old application using jquery version 1.4.2, which I cannot upgrade because at almost all places ".live" is used.
Thanks in advance.
As this comment says everything:
.live() was deprecated in jQuery 1.7, removed in 1.9. Please convert to .on()
So, Only if your radio elements are dynamically generated/created only in this case you should follow the event delegation event binding. you have to convert it with .on() event listener in conjunction with change event:
jQuery(document).on('change', 'input[type=radio]', function(e){
if($(this).hasClass('expandSectionSurvey')){
// do something
}else{
// do something else
}
});
As per comment jquery v1.4.x:
jQuery('input[type=radio]').live('change', function(e){
if($(this).hasClass('expandSectionSurvey')){
// do something
}else{
// do something else
}
});
Add this flie as reference: or use old one
type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
Use this, it may be helpful
$('input[type=radio].expandSectionSurvey').live('click',function(e){
alert('Test')
});
.live()
$(selector).live(events, data, handler); // jQuery 1.3+
.on()
$(document).on(events, selector, data, handler); // jQuery 1.7+
I have used the trigger() for checking the radio button after loading all the page content as the radio button value is coming from third party api.
I have make one option checked by default. So I used trigger() event for checking the radio button. The radio button have also it's click event.
In my code only radio button get selected but event is not firing.
my code is...
jQuery(document).ready(function($) {
jQuery("#btn_03").attr('checked', 'checked');
jQuery("#btn_03").trigger("change");
jQuery(".class input[type='radio']").live("change", function($) {
alert("clicked");
});
});
You need to assign the event handler before you trigger the event.
When you are actually triggering the event, you still haven't attached any listeners for that event. You are doing that in the next line. Thus the event change does get triggered but nothing happens on that event.
You can do it this way
jQuery(document).ready(function($) {
jQuery("#btn_03").attr('checked', 'checked');
jQuery(".class input[type='radio']").on("change", function($) {
alert("clicked");
});
jQuery("#btn_03").trigger("change");
});
Also use "on" to bind events instead of "live" as per the latest jQuery documentation
You need to trigger event after you attach event handler:
jQuery(".class input[type='radio']").on("change", function(e) {
alert("clicked");
});
jQuery("#btn_03").prop('checked', true).trigger("change");
Also use $.fn.on instead of long time ago deprecated $.fn.live. And it's better to set checked property instead of attribute.
You need to hook the change event before you actually call the change event. I've included a fiddle of the functionality you requested.
I've changed the change-event target to be the actual radio input since I did not have your html, but normally you would most likely want to use the name of the radio group to hook the event to, since those are usually coupled with a single behaviour - like so $("input[name='radioName']").change(function(){ // Change event code });
Sidenote: You can use '$' instead of of 'jQuery' to start an expression.
http://jsfiddle.net/du58fo3t/
$(document).ready(function() {
$("#btn_03").attr('checked', true); // Check radio button
// Hook change event on radio button
$("#btn_03").change(function() {
alert("clicked");
});
// Trigger change on radio button
$("#btn_03").trigger("change");
});
I am using jquery currently and I bind an event handler to checkboxes to bring users to a new page. The problem is that this does not scale well as binding 1500 checkboxes can cause ie8 on slower machines to think the script is not responsive when taking 2-3 seconds to bind these event handlers.
The only other solution I could think of was to use images instead of checkboxes so that they could be surrounded by an tag instead of having 1500+ event handlers bound. I do not prefer this solution as in general I dont think it's good practice to override the browser's default behavior/style.
Does anyone know a scalable way to bring users to a url when a checkbox is clicked?
The new jQuery recommandation is to put handlers on a common parent, like document, using .on:
$(document).on("click", ":checkbox", function () {
// your handler code
});
The handler isn't attached to your 1500 checkbox but the event naturally bubble to the document that check if the event target match your selector.
You could just use the 'onchange' method to check if it's 'checked' and then change the 'document.location.href' to the link you want.
<input type="checkbox" onchange="if(this.checked) document.location.href='http://www.google.com';"/>Go to google
$(document).ready(function() {
$(document).on('click', 'checkbox', function() { // <-- assuming your checkboxes are loaded asynchronously
window.location= "http://www.example.com";
});
});
I have a button that clears a list, the click on this button shows a dialog that asks for validation (Yes/No). What I want is to disable the "Clear" button after clearing the list (Click on Yes). Here's my code :
$('#clearBtn').click(function() {
$('#alert5').show();
$('#bg').show();
$('#Yes').click(function(){
$('.list1').empty();
$('#clearBtn').disable(true);
$('#clearBtn').click(function(e){
e.preventDefault();
});
$(".alert").fadeOut(250);
$(".alertbg").fadeOut(250);
});
});
the preventDefault() function doesn't seem to work.
First never nest event handlers.
$('#cleatBtn').click(function () {
$('#alert5').show();
$('#bg').show();
});
$('#Yes').click(function () {
$('.list1').empty();
$('#cleatBtn').attr('disabled', true);
$(".alert").fadeOut(250);
$(".alertbg").fadeOut(250);
});
If you just want to disable then use the following syntax
$('#cleatBtn').attr('disabled', true);
Remove the innermost event completely.. That is not required.
Use on to bind the events, if you want the button to be enabled but turn off the event handler using off
One more option you have is to apply a class to the button when you press yes and execute the code only when the class is not present.
$('#cleatBtn').click(function () {
if( !$(this).hasClass('active')) {
$('#alert5').show();
$('#bg').show();
}
});
$('#Yes').click(function () {
$('.list1').empty();
$('#cleatBtn').attr('disabled', true);
$('#cleatBtn').addClass('active');
$(".alert").fadeOut(250);
$(".alertbg").fadeOut(250);
});
To disable a button, call the prop function with the argument true on it:
$('#cleatBtn').prop("disabled", true);
e.preventDefault(); is the correct way of cancelling events. Some older browsers also expect a return type of false. Which I think will cause jQuery to call preventDefault()?
Here's a good answer: What's the effect of adding 'return false' to a click event listener?
I think your structure looks a bit odd. you don't need to attach click events within a click event.
Just attach them all separately on document.ready events. At the moment they are nested, then go back to trying to cancel your event. The dom tree might be confused by the way the events are nested.
Hope that helps.
Is it possible to trigger change event on a checkbox using javascript/jquery?
Something like this (I run triggerChange on click of a button):
<label><input type="checkbox" id="chk"/>Label for chk</label>
<script>
function triggerChange(){
$("#chk").trigger("change");
}
</script>
When I run the above code I get this error: "trigger is not a function".
That trigger is not a function error message indicates something else is at play. According to this SO question:
What happens when a jQuery selector wasn't found?
no.good.at.coding says:
Do note however that you must ensure that selector is a jQuery object!
Otherwise, you could get an error indicating that "trigger is not a
function".
It's likely that you have forgotten jQuery?
As for your implementation, you should be fine the way you are using it. But trigger should be used to trigger event methods on elements that have already been attached via jQuery. Check out my demo:
Fiddle:
With click event: http://jsfiddle.net/fS4R5/1/
Without click event: http://jsfiddle.net/fS4R5/2/
HTML:
<label><input type="checkbox" id="chk"/>Label for chk</label>
JS:
function triggerChange(){
$("#chk").trigger("change");
}
$("#chk").change(function() {
alert("triggered!");
});
triggerChange();
In jQuery, you can usually trigger an event by calling it's eventhandler method withoud any function parameters.
For example a click handler can be assigned as such:
$('#mything').click(function(e){dostuff});
the click event in itself can be triggered by simply running:
$('#mything').click();
I suspect this can be done for every existing event in jQuery.
Be sure that Input of Type checkbox is enabled, in case is disabled trigger will not fire event
//fire event
$('#ceckBoxId').click();
$('#ceckBoxId').trigger('click');
or change checkbox checked val
$('#ceckBoxId').prop('checked', true);
$('#ceckBoxId').prop('checked', false);
I think the preferred method since 1.9.1 is 'on'. Specially if you use dynamically added checkboxes.
Say you have a div with id='divCOntent' and on it is a checkbox with id='cballaut', you could do this
$('#divcontent').on('click', '#cballaut', function (e) {
alert(this.checked);
});
use prop method.
$('#myCheck').prop('checked', true);
$('#myCheck').prop('checked', false);