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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:08:19+00:00 2026-05-23T10:08:19+00:00

I have code like this <img onclick=getTitle(); title=myimg > function getTitle() { alert(jQuery(this).attr(title); }

  • 0

I have code like this

<img onclick="getTitle();" title="myimg" >  
function getTitle()  
{  
alert(jQuery(this).attr("title");  
}  

and its not working. Can somebody explain how to do this right. And what is wrong with this code.

  • 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-23T10:08:20+00:00Added an answer on May 23, 2026 at 10:08 am

    As @Neal says, if you use jQuery, use it properly.


    Nevertheless here is an explanation why your code does not work:

    this refers to window inside the function. You can set this explicitly by using .call():

    <img onclick="getTitle.call(this);" title="myimg" >
    <!-- `this` only refers to the element inside the handler --> 
    

    or you have to pass the element explicitly as argument:

    <img onclick="getTitle(this);" title="myimg" >
    

    Then you have to change your function too:

    function getTitle(element)  {  
        alert(element.getAttribute('title'));
    }  
    

    For more information about this type of event handling model, read this article on quirksmode.org about early event handlers.

    There is another one which explains this in the context of event handlers.

    The best is if you read all the articles about event handling and learn about the different models. You actually should not bind event handlers through HTML attributes anymore.

    For the sake of completeness, without jQuery, a better why is to attach the event handler to the DOM property. Assuming your image has an ID

    <img id="myimg" title="myimg" />
    

    the necessary JavaScript would be:

    function getTitle()  {  
        alert(this.getAttribute('title'));
    }  
    document.getElementById('myimg').onclick = getTitle;
    

    This code has to come after the element in the HTML.

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

Sidebar

Related Questions

I have some jQuery code something like this: $(document).ready(function() { $(img.off).click(function() { alert('on'); $(this).attr('class',
I have some code a little like this... <img src=... width=100 height=100 alt= onclick=FlipImg(1234)
I have a code that looks like this: <div id=wrapper> <img id=first class=images src=img/nike.jpg
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have jQuery code like this workig fine in FF, but totally fails in
I have this code: <ul> <li> <h3><a class=title href=page1.html>Post Title</a></h3> <ul> <li> <img src=imageOne.png
I have this piece of code: <ice:panelGroup menuPopup=menuPopup1 onclick=fireContextMenu(this, event);> <img src=/resources/images/external/bg-suppliers.gif> </img> </ice:panelGroup>
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code like this to move the player in my game left, right,

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.