views:

213

answers:

2

I am trying to pass the clicked event (event.target) to a function inside a function that is being called when clicked how would i do this?

function showGrid(){
 updateTag()
  }

function updateTag(){
    /*how do i get the event.target passed here? */
    alert(e.target) 
  }


$(".gridViewIcon").click(function(e) {
   showGrid();
  });
+3  A: 

Just pass the event argument object down through your function calls.

function showGrid(e){
        updateTag(e);
  }

function updateTag(e){
    /*how do i get the event.target passed here? */
    alert(e.target); 
  }


$(".gridViewIcon").click(function(e) {
   showGrid(e);
  });


To include the argument into a nested jQuery function, you can create a closure on the variable like so:

 function updateTag(e){

        alert(e.target); 

        ...
        var x = e;
        something.each( function(i) {  alert(x.target); } );

      }
womp
Thanks Much, How would i pass it to a .each function inside updateTag(e), i tried .each(function(i,e){ alert(e.target)} it does not work...
adardesign
Yeah, you need to create a closure on the variable to include it in a nested function like that. I'll add it to my answer.
womp
+1  A: 

Add e as a parameter for each function you need to pass it too.

function showGrid(e){
        updateTag(e)
  }

function updateTag(e){
    /*how do i get the event.target passed here? */
    alert(e.target) 
  }


$(".gridViewIcon").click(function(e) {
   showGrid(e);
  });
tj111
Thanks. How would i pass it to a .each function inside updateTag(e), i tried .each(function(e){ alert(e.target)} it does not work...
adardesign