I've click event bind to a class called ".reportfile" as follow.
$('body').on('click','.reportfile',function(e){
e.preventDefault();
e.stopPropagation();
var id=$(this).attr('id');
if(!$(this).hasClass('brc')) {
// Perform some action here.
}
});
Now I'm creating LI element dynamically with ".reportfile" class. Like this,
var inLi=$('<li>');
inLi.addClass('reportfile');
$(containerdiv).append(inLi);
Now when I try to click on the dynamic generated LI elements it only works on second click.
https://jsfiddle.net/mt7km8bz/
There is input box on the top of the UL to filter the list. This is where I'm creating new LI dynamically. LI element in filtered list has to be clicked twice to get it working.
The problem was the focus on the input box, on first click input box still have the focus so the click event on list doesn't trigger.
So on the first click it losses focus on the input box and on second click event get triggered.
Changes to following worked for me.
$('body').on('keyup','.searchable',function(){
// Some code
});
I guess jQuery click event works only after second click because the focus of the input.So I did a fool way that is trigger LostFocus Event using timer.
setTimeout(function(){
$('.searchable').blur();
},1500);
This is the code...
https://jsfiddle.net/2hkn2jpk/
try this.....
$(document).on('click','.reportfile',function(e){
var id=$('.reportfile').attr('id');
if(!$('.reportfile').hasClass('brc')) {
// Perform some action here.
}
});
change this to keypress , Its working.
I cheked in fiddle.
Jsfiddle
$( ".searchable" ).keypress(function(){
$('.ul2').html('');
var value=$(this).val().trim().toLowerCase();
if(value=="") {
$('.ul1').show();
$('.ul2').hide();
} else {
$('.ul1').hide();
$('.ul2').show();
}
$('.ul1').find('.reportfile').each(function(index){
var title=$(this).attr('title').toLowerCase();
if(title.indexOf(value)>=0) {
var LIin=$("<li>");
LIin.addClass('reportfile');
LIin.text(title);
$('.ul2').append(LIin);
}
});
});
Related
So I'm developing a script that makes use of a dropdown (class typeahead_list) and a textbox (class et_email) in Bootstrap. Whenever the text box loses focus, I need the dropdown to close. But if someone clicks on one of the list items in the dropdown (class et_li), the contents of that list item must populate the text box before closing the dropdown.
Note: All of the lis are dynamically generated. I don't know if that makes a difference or not.
This code puts the li contents into the textbox, but does not close the dropdown.
$( ".et_email" ).focusout(function() {
$(".typeahead_list").on("click", "li", function() {
$(".et_email").val($(this).text());});
//$(".typeahead_list").hide();
});
This code closes the dropdown, but does not put the contents of the li into the text box.
$( ".et_email" ).focusout(function() {
$(".typeahead_list").on("click", "li", function() {
$(".et_email").val($(this).text());});
$(".typeahead_list").hide();
});
Any idea on how to fix this? Where is the conflict coming from?
UPDATE:
Alex Cassady suggested the following fix:
$(".typeahead_list").on("click", "li", function() {
$(".et_email").val($(this).text()); // copy value
$(".typeahead_list").hide(); // then hide
});
Thank you for your help. I've looked into a solution similar to this before. However, I was having an issue because I need the dropdown to close whenever the email box loses focus for any reason at all... while still responding to the li click. With this example, the dropdown only closes when someone clicks on the li.
But if I try to add another handler for $( ".et_email" ).focusout() within the $(document).ready(), it completely invalidates the effects of this function. It's like $( ".et_email" ).focusout() and $(".typeahead_list").on("click", "li", function(){}) can't live in the same universe together. There is some kind of conflict.
Basically, the rule I need to implement is: Always close the dropdown when et_email loses focus for any reason... but if focus is lost because of an li click with the typeahead_list div, then populate the text box with the contents of that li before closing the dropdown.
You're binding a click listener to an element which will be hidden right away and so can not be clicked! The reason it works if you comment the hide() line is because it can be clicked.
If you just want the text before hiding:
$( ".et_email" ).focusout(function() {
$(".et_email").val($(".typeahead_list").text());
$(".typeahead_list").hide();
});
EDIT:
Alright I think I have a better understanding of what you're trying to do. This is not a clean solution, but will do the job.
Have a global variable 'focusLost' or anything, It'll be set to true every time .et_email loses focus and set to false in a second. In .typeahead_list click listener we check whether focusLost is true.
var focusLost = false;
$('.typeahead_list').on('click', function () {
if (focusLost) {
$('.et_email').val(($(this).text()));
$(this).hide();
}
});
$('.et_email').focusout(function () {
focusLost = true;
setTimeout(function () {
$('.typeahead_list').hide();
focusLost = false;
}, 1000);
});
jsfiddle DEMO
As A. Wolff said, don't nest handlers. The focusout event is binded to all .et_email, but the click event isn't binded to the .typeahead_list until the focusout event is triggered.
The .typeahead_list is being hidden before the value can be changed. Focusout is the same as clicking outside of .et_email and it's being called first.
Removing the focusout event handler completely should solve the problem.
$(".typeahead_list").on("click", "li", function() {
$(".et_email").val($(this).text()); // copy value
$(".typeahead_list").hide(); // then hide
});
Edit: You could also try copying the value in a callback function for $.hide()
$(".typeahead_list").on("click", "li", function() {
$(".typeahead_list").hide( function() {
$(".et_email").val($(this).text()); // copy value
}); // then hide
});
I am trying to achieve the result when i am typing inside input the list will drop down.
if the list is clicked then will give out an alert.
in the same time if the input is focus out , it will hide the list.
But the problem is when on click , it doesn't work.
$("input").on("keyup",function(){
$(".abc li").show();
});
$(".abc li").on("click",function(){
alert("123");
});
$("input").focusout(function(){
$(".abc").find("li").hide();
});
Reference to my code
http://jsfiddle.net/8t7erade/
Your click event is never getting called because the li is hidden by your focusout event before it can be triggered.
You could set a timeout to allow for the click to be registered:
$("input").focusout(function () {
setTimeout(function () {
$(".abc").find("li").hide()
}, 100)
});
demo
How about setting (short) time out on blur (focus out)?
Like this:
$("input").on("keyup",function(){
$(".abc li").show();
});
$(".abc li").on("click",function(){
alert("123");
});
$("input").focusout(function(){
setTimeout(function(){$(".abc").find("li").hide();}, 150);//i set 150 just as example
});
Problem is that blur occurs first, and at that time elements got hidden and mouse event is passed to other (visible) element.
How to prevent click another button if first is clicked...
Example
http://jsfiddle.net/C5AVH/3/
$(function(){
$('.vote_like').one('click',function(){
$('.vote_dislike').removeClass('vote_dislike');
alert('Done!')
});
$('.vote_dislike').one('click',function(){
$('.vote_like').removeClass('vote_like');
alert('Done!');
});
});
Like -
Dislike
When you click Like button i want disable click on Dislike button and inversely...
im try with removing class but seems that not working...
$('.vote_like').removeClass('vote_like');
You can remove the click handler
$(function () {
$('.vote_like').one('click.like', function () {
$('.vote_dislike').off('click.like');
console.log('like!')
});
$('.vote_dislike').one('click.like', function () {
$('.vote_like').off('click.like');
console.log('dislike!');
});
});
Demo: Fiddle
Because you've attached the .one handler to each button, it will still be executed at most one time according to the jquery docs. To prevent the click you must remove the handler after one is clicked.
$('.vote_like').one('click',function(){
$('.vote_dislike').off();
alert('Done!')
});
$('.vote_dislike').one('click',function(){
$('.vote_like').off();
alert('Done!');
});
});
Like -
Dislike
But better yet, why not just attach the one handler to both elements and check which was clicked:
$(function(){
$('.vote_like,.vote_dislike').one('click',function(){
if($(this).is('.vote_like')){
//set data for like
}
else{
//set data for dislike
}
//make ajax call
});
Anchors don't have a way to disable them, so you'd either need to remove the anchor or set a boolean in your javascript to track if it's been clicked.
Or, you can convert them into actual button elements, play with the disabled state.
Or you can use jquery to add custom data attributes to the anchor to track if it's "disabled"
Demo
$(function(){
$('.vote_like, .vote_dislike').on('click',function(){
$(this).siblings('.vote_like, .vote_dislike').add($(this)).prop('disabled',true);
if ($(this).hasClass('vote_like')) {
/* Do like things */ alert('like');
}else{
/* Do dislike things */ alert('dislike');
}
});
});
Can use one handler for both buttons and remove click handler within it for both
var btns=$('.vote_like, .vote_dislike').on('click',function(e){
e.preventDefault();
var isLikeBtn=$(this).is('.vote_like');
/* remove click handler for both, remove class from other button */
btns.off('click').not(this).removeClass( isLikeBtn ? 'vote_dislike' : 'vote_like');
});
Since using off on both would be equivalent of using one
I used the methods in this question:
change div class onclick on another div, and change back on body click
So here's my jQuery function:
jQuery('.checkbox_wrapper').on('click', function(e){
jQuery(this).parent()
.toggleClass('not_selected')
.toggleClass('selected');
});
However it doesn't seem to be working properly. It takes multiple clicks before the class changes.
See my jsfiddle:
http://jsfiddle.net/7A3vw/
I cut it down to the bare essentials thinking it might be conflicting javascript, but even with the single function it takes multiple clicks before the class actually changes. Because the production environment has 1 click toggle a hidden checkbox, multiple clicks is not reasonable.
Could someone help me figure out what's causing this issue?
The click function fires twice, once for the image, and once for the input, as both will bubble to the parent element, and firing twice reverts the classes again (proof).
Just target the image instead, as that is what you're really trying to click, not the parent :
jQuery('.deck_card img').on('click', function (e) {
jQuery(this).closest('div').parent().toggleClass('not_selected selected')
});
FIDDLE
i guest you need the checkbox checked together with the toggling of your div.
$(document).ready(function(e) {
$('.checkbox_wrapper').on('click', function(e){
var checked = $(this).find('input[type="checkbox"]').is(":checked");
if(checked){
jQuery(this).parent().addClass('selected').removeClass('not_selected');
}else{
jQuery(this).parent().addClass('not_selected').removeClass('selected');
}
});
});
Your code is triggering click event twice. So use .preventDefault()
This makes the default action of the event will not be triggered.
$('.checkbox_wrapper').on('click', function(e){
$(this).parent()
.toggleClass('not_selected')
.toggleClass('selected');
e.preventDefault(); // prevent the default action to be
}); // triggered for next time
Check this JSFiddle
try this
jQuery(document).on("click",'.checkbox_wrapper', function(e){
jQuery(this).parent()
.toggleClass('not_selected')
.toggleClass('selected');
});
Multiple Clicks are getting triggered because you are using class selector. You need to use not to exclude extra elements :
jQuery("div.checkbox_wrapper :not('div.checkboxdiv')").on('click', function(e){
jQuery(this).parent()
.toggleClass('not_selected selected')
});
Here is a FIDDLE.
I have a div element containing a checkbox, when I click the div I want the checkbox to toggle.
I did this by assigning the following code to $(#div).click:
$(this).find(":checkbox").prop("checked", !$(this).find(":checkbox").prop("checked"));
The problem now is that if the checkbox itself is clicked, the code above is still executed and thus the checkbox stays in the same state.
How would I fix this?
The event is bubbling up, you need to stop event propogation, one way is
$('#myDiv :checkbox').click(function(e){
e.stopPropagation();
});
Edit
In case you want to handle the event at div use this
$('div').click(function(e) {
// check if the event was triggered by an input box
if (e.target.nodeName != "INPUT") {
$(this).find(":checkbox").prop("checked", !$(this).find(":checkbox").prop("checked"));
}
// do something else
alert('hey there!');
});
Demo - http://jsfiddle.net/QnbhX/
onclick() of div element, attach a javascript function which toggles the checkbox output. Isn't that a simpler way?
You should use a better selector like :
$('#myDiv').find(":checkbox").prop("checked", !$(this).find(":checkbox").prop("checked"));