最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pass the e.target to a function thats called when element is clicked - Stack Overflow

programmeradmin0浏览0评论

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

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();
  });
Share Improve this question asked Sep 17, 2009 at 17:50 adardesignadardesign 35.9k15 gold badges66 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

      }

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);
  });
发布评论

评论列表(0)

  1. 暂无评论