Get text string of event handler in button.

Posted on 2007-08-06
Last Modified: 2008-01-09

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

<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.
   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!

Question by:paulmedina

    Author Comment

    Never mind I figured it out.

    I used:

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

    Thanks everyone.
    LVL 19

    Expert Comment

    Will this work?

    <script type="text/javascript">
    function someFunction(str, btnObj, something)

    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(/}$/,"");


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

    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.
    LVL 19

    Expert Comment

    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.
    LVL 63

    Expert Comment

    How about this:

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

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

    LVL 1

    Accepted Solution

    PAQed with points refunded (500)

    EE Admin

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now