-4

I am using one click function() to render some chart on my project.

The code is as below

$('.abc').click(function() {
       checkGraph();
});

But when i resize the window then this click event is not firing. Means my chart us not getting rendered.

I tried some of the things as below but none worked

Approach 1

$(document).on('click','.abc',function(){
  checkGraph();
});

Approach 2

var crclick = (function(){
            $('.abc').click(function() {
                checkGraph();
            });
    });
    crclick();
    $(window).resize(function(){
        console.log("Window resized New");
        crclick();
    });

In approach 2, i am getting console output but click function is not working.

Gags
  • 3,759
  • 8
  • 49
  • 96

3 Answers3

0

use on window resize. You are just binding the click on resize.

 $(window).resize(function(){
    checkGraph();
});
Sumit
  • 1,619
  • 1
  • 11
  • 24
  • I was also, but then not. Weird. Anyway your solution may click a LOT – mplungjan Apr 20 '15 at 09:52
  • Who is downvoted please comment and why ? Let me learn where I am wrong ? – Sumit Apr 20 '15 at 09:53
  • Yes I know but this is what he want! – Sumit Apr 20 '15 at 09:54
  • This may is not very clear. Someone who tries to maintain this would have no idea why you're clicking a button on window resize, but a direct call to checkGraph() makes sense. Also `.trigger` could also cause other click events that dont need to fire on resize to fire erroneously. – Dylan Watt Apr 20 '15 at 09:56
0

Why would click fire on a resize? You are only assigning the handler.

Later you can click it using $(".abc").click(); or $(".abc").trigger("click");

Be aware you likely want to only click AFTER a resize

https://stackoverflow.com/a/15170104/295783

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

The resize and click events are separate. If you want both to call checkGraph, bind them individually to call checkGraph(). If you were binging to events to the same element you could do it in once call.

$('.abc').click(function() {
   checkGraph();
});

$(window).resize(function(){
    checkGraph();
});

I wouldn't do the .trigger method mentioned in other answers, it's not very clear code, with potential other consequences.

Dylan Watt
  • 3,357
  • 12
  • 16