Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:27:19+00:00 2026-06-03T05:27:19+00:00

Is there a way to retrieve the element source of an inline javaScript call?

  • 0

Is there a way to retrieve the element source of an inline javaScript call?

I have a button like this:

<button onclick="doSomething('param')" id="id_button">action</button>

Note:

  • the button is generated from server
  • I cannot modify the generation process
  • several buttons are generated on the page, I have control only on client side.

What I have tried:

function doSomething(param){
    var source = event.target || event.srcElement;
    console.log(source);
}

On firebug I get event is not defined

Edit:
After some answers, an override of the event handling using jQuery is very acceptable. My issue is how to call the original onClick function with it’s original prameters, and without knowing the function name.

code:

<button onclick="doSomething('param')" id="id_button1">action1</button>
<button onclick="doAnotherSomething('param1', 'param2')" id="id_button1">action2</button>.
<button onclick="doDifferentThing()" id="id_button3">action3</button>
.
.
and so on..

So the override would be:

$(document).on('click', 'button', function(e) {
  e.preventDefault();
  var action = $(this).attr('onclick');
  /**
   * What to do here to call
   * - doSomething(this, 'param'); if button1 is clicked
   * - doAnotherSomething(this, 'param1', 'param2'); if button2 is clicked
   * - doDifferentThing(this); if button3 is clicked
   * there are many buttons with many functions..
   */
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:27:21+00:00Added an answer on June 3, 2026 at 5:27 am

    You should change the generated HTML to not use inline javascript, and use addEventListener instead.

    If you can not in any way change the HTML, you could get the onclick attributes, the functions and arguments used, and “convert” it to unobtrusive javascript instead by removing the onclick handlers, and using event listeners.

    We’d start by getting the values from the attributes

    $('button').each(function(i, el) {
        var funcs = [];
    
    	$(el).attr('onclick').split(';').map(function(item) {
        	var fn     = item.split('(').shift(),
            	params = item.match(/\(([^)]+)\)/), 
                args;
                
            if (params && params.length) {
            	args = params[1].split(',');
                if (args && args.length) {
                    args = args.map(function(par) {
                		return par.trim().replace(/('")/g,"");
                	});
                }
            }
            funcs.push([fn, args||[]]);
        });
      
        $(el).data('args', funcs); // store in jQuery's $.data
      
        console.log( $(el).data('args') );
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button onclick="doSomething('param')" id="id_button1">action1</button>
    <button onclick="doAnotherSomething('param1', 'param2')" id="id_button1">action2</button>.
    <button onclick="doDifferentThing()" id="id_button3">action3</button>

    That gives us an array of all and any global methods called by the onclick attribute, and the arguments passed, so we can replicate it.

    Then we’d just remove all the inline javascript handlers

    $('button').removeAttr('onclick')
    

    and attach our own handlers

    $('button').on('click', function() {...}
    

    Inside those handlers we’d get the stored original function calls and their arguments, and call them.
    As we know any function called by inline javascript are global, we can call them with window[functionName].apply(this-value, argumentsArray), so

    $('button').on('click', function() {
        var element = this;
        $.each(($(this).data('args') || []), function(_,fn) {
            if (fn[0] in window) window[fn[0]].apply(element, fn[1]);
        });
    });
    

    And inside that click handler we can add anything we want before or after the original functions are called.

    A working example

    $('button').each(function(i, el) {
        var funcs = [];
    
    	$(el).attr('onclick').split(';').map(function(item) {
        	var fn     = item.split('(').shift(),
            	params = item.match(/\(([^)]+)\)/), 
                args;
                
            if (params && params.length) {
            	args = params[1].split(',');
                if (args && args.length) {
                    args = args.map(function(par) {
                		return par.trim().replace(/('")/g,"");
                	});
                }
            }
            funcs.push([fn, args||[]]);
        });
        $(el).data('args', funcs);
    }).removeAttr('onclick').on('click', function() {
    	console.log('click handler for : ' + this.id);
      
    	var element = this;
    	$.each(($(this).data('args') || []), function(_,fn) {
        	if (fn[0] in window) window[fn[0]].apply(element, fn[1]);
        });
      
        console.log('after function call --------');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <button onclick="doSomething('param');" id="id_button1">action1</button>
    <button onclick="doAnotherSomething('param1', 'param2')" id="id_button2">action2</button>.
    <button onclick="doDifferentThing()" id="id_button3">action3</button>
    
    <script>
    	function doSomething(arg) { console.log('doSomething', arg) }
        function doAnotherSomething(arg1, arg2) { console.log('doAnotherSomething', arg1, arg2) }
        function doDifferentThing() { console.log('doDifferentThing','no arguments') }
    </script>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an element like this... <math xmlns=http://www.w3.org/1998/Math/MathML> <mo class=symbol>α</mo> </math> Is there
is there a way to retrieve only the immediate children found by a call
I have a plugin which is applied on few element. Is there any way
Is there any way to retrieve the index of the element on which a
Is there a way to atomically pop (remove and retrieve) a random element with
Is there a way to retrieve first n elements from a Dictionary in C#?
is there a way to retrieve type T from IEnumerable<T> through reflection? e.g. i
Is there any way to retrieve the names of the stored procedures by some
Is there a way to retrieve the auto generated key from a DB query
Is there a way to retrieve path information from a file descriptor or FILE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.