Get text string of event handler in button.

Hi,

I have a button whose onclick event calls a javascript function:

e.g.
<input type="button" id="aButton" onClick="someFunction( 'textvalue', this, null )">

How can I get the text string of that function call?

I've tried:

var thecode ;
var somebutton = document.getElementById('aButton');
if( document.all )
   thecode = somebutton.getAttribute('onclick') ; //in I.E.
else
   thecode = somebutton.onclick; //mozilla

However, this just causes the button's onclick event to get fired instead of returning the text.

Any ideas?

Thanks in advance!

Paul
paulmedinaAsked:
Who is Participating?
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
paulmedinaAuthor Commented:
Never mind I figured it out.

I used:

if( document.all )
   thecode = somebutton.getAttribute('onclick').toString() ; //in I.E.
else
   thecode = somebutton.onclick.toString(); //mozilla


Thanks everyone.
0
 
dakydCommented:
Will this work?

<html>
<head>
<script type="text/javascript">
function someFunction(str, btnObj, something)
{
  alert(str);
}

function getFuncStr(id)
{
  var someButton = document.getElementById(id);
  var strFunc = someButton.onclick.toString();

  // both IE & Firefox wrap it in some other function
  // we need to strip it out
  strFunc = strFunc.replace(/^function [^{]+\s?{/, "").replace(/}$/,"");

  alert(strFunc);
}
</script>
</head>

<body>
<form>
  <input type="button" id="aButton" value="calls someFunction" onclick="someFunction('textvalue',this, null);">
  <input type="button" value="get aButton's onclick" onclick="getFuncStr('aButton');">
</form>
</body>
</html>

The only thing I think you did wrong was to not use the toString() for the onclick.  In this case, the important part is what's in the getFuncStr() function.  I passed in the id of the button you want, but you can make it work just as easily if you can identify the button without passing in the ID.  Hope that helps.
0
 
dakydCommented:
Oops, looks like I was a couple of minutes late on this one.  You can ask in Community Support to get this closed out for you.
0
 
ZvonkoSystems architectCommented:
How about this:


<body>
<form>
<input type="button" id="aButton" onClick="someFunction( 'textvalue', this, null )">

<input type="button" value="getString" onClick="alert((this.form.aButton.onclick+'').match(/'([^']+)'/)[1])" >
</form>
</body>



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.