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

  • Home
  • SEARCH
  • 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 1958388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:18:44+00:00 2026-05-17T08:18:44+00:00

I am calling a function test() on an anchor tag like this:- <a class=link

  • 0

I am calling a function test() on an anchor tag like this:-

<a class="link" onclick="test();">Hello</a>

And I am trying to access the class of the anchor inside the function test():-

function test()
{
  alert($(this).attr("class"));
}

But this alerts undefined. I know that calling onclick=test(this); instead and having the following function works:-

function test(obj)
{
  alert($(obj).attr("class"));
}

But, I wanted to access the properties of the element (on which the function is called) without passing a this. Just be able to get/set properties of the element (on which the function is called). Is it possible? How to do this?

  • 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-05-17T08:18:44+00:00Added an answer on May 17, 2026 at 8:18 am

    In this scenario we need to make sure that HTML element owns that function which you wanna call upon click. What u actually doing is that u are using onclick attribute which just takes string parameter and completely oblivious of everything else.

    In order to make HTML element owner of the function you need to map onclick property completely with your own custom function. In JavaScript you do that like that

    element.onclick = doSomething;
    

    So our code will be something like

    <div id="element">Click</div>
    <script>
    function doSomething(){
        this.style.color= "#000000"
    }
    document.getElementById("element").onclick = doSomething;
    </script>
    

    here is the working example http://www.jsfiddle.net/VMCmb/1/.

    Also please take a look at http://www.quirksmode.org/js/this.html.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm calling a function from an input radio button and have this: onClick=\changeBilling('.$results['id'].', this.value)\
I am calling function with LoadData.send_later(:test). LoadData is my class and test is my
I got a class like this (this is an abstract example): class Example(){ function
Class test{ function test1() { echo 'inside test1'; } function test2() { echo 'test2';
I'm trying to return a value from (transport) to the calling function, but can't
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Hi I'm calling this function: function getCoordenadas() { var coordenadas = new Array(); $.post(
So i have click function and i'm calling this function couple times on same
I have this opaque type type_t and a function prototype like foo(type_t *t) and
After calling the function test , I print the dtr1 array. I am expecting

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.