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 9231343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:02:32+00:00 2026-06-18T06:02:32+00:00

The way I know: HTML: <input type=button id=msg onclick=foo(this); /> JS: function foo(elementObj) {

  • 0

The way I know:

HTML:

<input type="button" id="msg" onclick="foo(this);" />

JS:

function foo(elementObj) { elementObj.toggle(); }

Is there any way to get reference to elementObj inside foo() without passing it as function argument?

How I want to do:

HTML:

<input type="button" id="msg" onclick="foo();" />

JS:

function foo() { elementObj = <any way I can get ref to html element here?>; elementObj.toggle(); }

P.S.

Call function like this: $("#msg").click(function(elementObj ) {elementObj.toggle();}

Is not what I need.

  • 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-18T06:02:33+00:00Added an answer on June 18, 2026 at 6:02 am

    Is there any way to get reference to elementObj inside foo() without passing it as function argument?

    Yes, just use proper unobtrusive jQuery event handler attaching such as .on, or its shorthands:

    $('#msg').click(function() {
        //`this` references the clicked DOM element #msg
    });
    

    Or if you need to use the function foo elsewhere..

    function foo(eventObject) { 
        //`this` reference the #msg element
    }
    $('#msg').click(foo);
    

    As per OP edit:

    function foo() {
       elementObj = <any way I can get ref to html element here?>;
       elementObj.toggle();
    }
    

    Yeah just use the ID selector $('#msg') or $(this) if you want to reference the clicked element.

    function foo() {
       var elementObj = $(this);
       elementObj.toggle();
       //or more directly $(this).toggle()
    }
    $('#msg').click(foo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a way to add an input field (or any type
I don't know what's the best way to doing this. On my application.html.erb I
Is there any way to know in Servlet is JS is enable or not
Consider this HTML snippet: <input type='file' id='fileUpload' /> To get access to this control
I have these HTML and CSS: #siteInfo input[type=button] { background: none repeat scroll 0
Does anyone know of a way to contain a nonbreaking space in an html
I would know if there is a way to know if an element of
Is there a way to know where is set a given PHP config property?
Is there a way to know if Visual C++ 2008 SP1 Redistributable Package is
is there a way to know when an object will be disposed by GC?

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.