0

Click event is not working on firefox but it's working in chrome. why?

setTimeout(function(){ 

$("#id").prev('div').find('button').find('div.classname').click(function(){
alert("Testing");
});


}, 3000);
Kanaka k
  • 95
  • 2
  • 12
  • 1
    Why are you binding it after 3 seconds? Why are you not binding the click to the button, but its child? Why is there a block element in your button? – epascarello Sep 28 '18 at 13:59
  • Check if firefox is returning an element or not ? Type "$("#id").prev('div').find('button').find('div.classname')" in firefox console and see if there any element with that selector. – Mukesh Verma Sep 28 '18 at 14:01
  • 1
    The problem must be something else. jQuery and it's click method are stable enough to work properly on all popular browsers. – Ram Sep 28 '18 at 14:01
  • 4
    You can't have a DIV inside a button, see https://stackoverflow.com/questions/15885444/why-cant-a-button-element-contain-a-div – Barmar Sep 28 '18 at 14:02
  • @undefined not if you don't follow standards. That's why standards are important. Despite the fact that html is often very forgiving (in most borwsers), when you go outside the standards you can get weird behavior. – Mark Baijens Sep 28 '18 at 14:20

1 Answers1

1

If you bind the click event to the button itself, and not the div inside it, that will work :

setTimeout(function(){ 

$("#id").prev('div').find('button').click(function(){
alert("Testing");
});


}, 3000);

Fiddle http://jsfiddle.net/aur7dwL4/

However, as noted above by @Barmar, having a div inside of a button is invalid HTML markup.

jwebb
  • 1,119
  • 12
  • 23