3

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.

11
  • 3
    Can you share executable demo/snippet or JSFiddle ? May be because you have certain condition in click handler.. Commented Apr 6, 2016 at 5:40
  • Edited my question with jsFiddle link. Commented Apr 6, 2016 at 6:05
  • becoz the first click that get out of focus on search, then it clicked. that is your li does't recog first click..once the input value changed ,then it reacts the on click... Commented Apr 6, 2016 at 6:15
  • Try changing $('.ul1').find('.reportfile').each(function(index) to $('.ul1').find('.reportfile').each(function(title) and var title to title, finally, remove this as well $('.ul2').html('');. Is this what you want? Also according to css-tricks e.stopPropagation(); is considered bad practice. css-tricks.com/dangers-stopping-event-propagation Commented Apr 6, 2016 at 6:17
  • 1
    jsfiddle.net/3wzb4yqs Commented Apr 6, 2016 at 6:24

4 Answers 4

3

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
});
Sign up to request clarification or add additional context in comments.

Comments

1

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/

1 Comment

Yeah, indeed! It has to do with input focus!
0

try this.....

$(document).on('click','.reportfile',function(e){

    var id=$('.reportfile').attr('id');
    if(!$('.reportfile').hasClass('brc')) {
         // Perform some action here.
    }
});

Comments

0

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);
        }
    });
});

1 Comment

You forgot to add e as the parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.