469,343 Members | 5,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How to get the function name and parameters a (onclick) event handler invoked

Here is a simple code :

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" id="ck1" onclick="func1('para1')">
  2.  
  3. <script>
  4.   alert(ck1.onclick);   //result of this:
  5.   /////////////////////  
  6.    function anonymous () {
  7.          func1('para1')
  8.    }
  9.   /////////////////////
  10. </script>
and when i try to get the type of 'ck1.onclick' obj use typeof func like this:
alert(typeof(ck1.onclick)), and the result is : "function" .
So,how can i get the function name and parameters on onclick event handler ?
Nov 19 '07 #1
5 7430
gits
5,390 Expert Mod 4TB
hi ...

could you tell wherefor you would need this? i don't know a way to do it, but may be we might work around it ...

kind regards
Nov 19 '07 #2
hi ...

could you tell wherefor you would need this? i don't know a way to do it, but may be we might work around it ...

kind regards
Actually,i may have an function called showFuncNameAndPara() ,and i wish i could get the function name and parameters on its onclick event handler ,just like this :
Expand|Select|Wrap|Line Numbers
  1. funtion showFuncNameAndPara () {
  2.     el = document.getElementById("ck1");  //now i get the element and then wish to
  3.    //get its onclick attribute value , 
  4.    //i wannar get a the function name "func1" on onclick event handler and the   parameters of the function
  5.    //yet,when i give this code :
  6.   alert(el.onclik);   //i try to show onclik attribute on el,it alert me :
  7.  //function anonymous { func1('para1') } .
  8.   //but it's a string to me yet, and el.onclik returns a 'function' type , does it have
  9.  //some property just like name 、parameters or something else i could get ?
  10. }
  11.  
Thank you.
Nov 19 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

ok have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. function test(a, b) {
  2.     // code here
  3. }
  4.  
  5. function get_func_signature(func) {
  6.    var f = func.toString();
  7.    var t = '';
  8.  
  9.    var a = f.match(/[ ](.+)[(](.+)[)]/i);
  10.  
  11.    t = 'function-name: ' + a[1];
  12.    t+= '\nparameters: '  + a[2];
  13.  
  14.    return t;
  15. }
  16.  
  17. alert(get_func_signature(test));
  18.  
it parses the string with a regExp and returns the name and params ... func would be your onclick-function-reference for your case :)

kind regards
Nov 19 '07 #4
hi,it looks likely to get a function name and parameters by parsing string is a good way or only.still,it's a good approach . thanks a lot!
Nov 19 '07 #5
gits
5,390 Expert Mod 4TB
hi,it looks likely to get a function name and parameters by parsing string is a good way or only.still,it's a good approach . thanks a lot!
hi ...

as far as i know ... it is the only way ... you might use the name-property of the function to retrieve the name and its length-property to retrieve the number of params but i don't know another way to retrieve the names of the params ...

kind regards
Nov 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Mark Kurten | last post: by
10 posts views Thread by ljlolel | last post: by
7 posts views Thread by Darko | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.