paulmedina
asked on
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('a Button');
if( document.all )
thecode = somebutton.getAttribute('o nclick') ; //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
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('a
if( document.all )
thecode = somebutton.getAttribute('o
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
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.toStrin g();
// 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('tex tvalue',th is, null);">
<input type="button" value="get aButton's onclick" onclick="getFuncStr('aButt on');">
</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.
<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.toStrin
// both IE & Firefox wrap it in some other function
// we need to strip it out
strFunc = strFunc.replace(/^function
alert(strFunc);
}
</script>
</head>
<body>
<form>
<input type="button" id="aButton" value="calls someFunction" onclick="someFunction('tex
<input type="button" value="get aButton's onclick" onclick="getFuncStr('aButt
</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.
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.
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.on click+''). match(/'([ ^']+)'/)[1 ])" >
</form>
</body>
<body>
<form>
<input type="button" id="aButton" onClick="someFunction( 'textvalue', this, null )">
<input type="button" value="getString" onClick="alert((this.form.
</form>
</body>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I used:
if( document.all )
thecode = somebutton.getAttribute('o
else
thecode = somebutton.onclick.toStrin
Thanks everyone.