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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:58:09+00:00 2026-05-19T09:58:09+00:00

I have an <img src=blabla.png onclick=someFunction();> tag that I want to replace with an

  • 0

I have an <img src="blabla.png" onclick="someFunction();"> tag that I want to replace with an <a> tag (created dynamically).

I want to move the onclick from the to the but when I read attr("onclick") it returns:

function onclick(event) {
  someFunction();
}

How can I extract only the someFunction(); call and set this to the onclick of the <a> tag?

After the answer I used it like this:

$(function(){
    $(".content img[src*=images]").each(function(){
        $(this).after("<a href=# class='button'></a>");
        $(this).next().text($(this).attr("alt"));
        $(this).next().click(this.onclick);
        /* remove old img */
        $(this).remove();
    });
});
  • 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-19T09:58:09+00:00Added an answer on May 19, 2026 at 9:58 am

    Use the this.onclick instead of $(this).attr(‘onclick’). It will be a function type and you can simply set the a.onclick = img.onclick.

    Ideally however the image would have a click handler and would be bound unobtrusively.

    var someFunction = function(){};
    $('img').click(someFunction);
    

    Then you could use the same function on the a

    $('a').click(someFuncion);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that i have img tag like this, <img src = myimage.jpg width
I want to call a Servlet from img src. I have defined a Servlet
I have this regex in JavaScript: var val = val.replace(/[:)]/g, <img src = \/img/smile.png\
I want to use the same string in my img src in my onclick
I have a code like this: <a href=somewhere.html> <img src=blabla> <div class=wrap> <div id=id1
I have a script that echo's usernames an inserts that into img src. This
I'm presenting a simple animation using img.src replace and the <canvas> tag. At present
Let's say I have, <div> <img src='aaBB_a.png' /> </div> <div> <img src='aaa_a.png' /> </div>
I have following code (php), it will match img-src and replace with new url
I need a regular expression that can match any url in img src tag

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.