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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:10:28+00:00 2026-05-15T07:10:28+00:00

I have a webapplication where (as in many other ones out there) you click

  • 0

I have a webapplication where (as in many other ones out there) you click on an image to do something, for instance, mark an entry, send a mail, flag something…

Short, clicking on the image is supposed to call an action (via javascript, but that’s not the point).

I was wondering, what is the “right” way to do this?

  • <a>-tag? Hmm… actually it is not a link…
  • <button>? Because obviously a button is the semantic element for calling an action…
  • <div>?

Any hints?

  • 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-15T07:10:29+00:00Added an answer on May 15, 2026 at 7:10 am

    Short Answer

    Use an <img> – not a button or an anchor or an input – as the rest suggest that the element is interactive, even without JavaScript.

    Long Answer

    clicking on the image is supposed to call an action (via javascript, but that’s not the point).

    I disagree; that is the point 🙂

    Because the clicking activates JS-only features, your image should only be available in a JS environment.

    As such the proper way is to insert it with JavaScript; while an HTML document should be semantically correct, a DOM structure doesn’t really need to be semantically correct, so which element you use becomes irrelevant.

    The Wrong Way

    <div>
        Click on the image to do something: <a href="javascript:wtv()" style="background-image:url(...)">&nbsp;</a>
    </div>
    <div>
        Click on the image to do something: <input type="image" onclick="wtv()" src="..." />
    </div>
    <div>
       Click on the image to do something: <img onclick="wtv()" src="..." />
    </div>
    <div>
        Click on the image to do something: <button onclick="wtv()"><img onclick="wtv()" src="..." /></button>
    </div>
    

    These are all wrong because a user who doesn’t have JavaScript sees these items and can’t use them.

    Of all of these, I’d say the <img> is the lesser evil, as it doesn’t suggest an interactive element. The greatest evil is using the <a> as an anchor should be a hyperlink to another document, and you should never, ever use the javascript: protocol.

    You’ll still have the same problem when you add the JavaScript event handlers externally:

    /* external .js file */
    document.getElementById("myButton").onclick = wtv;
    
    <!-- HTML document -->
    <div id="myButtonParent">
        Click on the image to do something: <a id="myButton" href="#" style="background-image:url(...)">&nbsp;</a>
    </div>
    

    As, again, you still have the (non-functioning) hyperlink available to those users who don’t have JavaScript.

    Instead

    Instead, insert the whole damn thing using DOM scripting! I’m going to use an <img> with an onclick event:

    /* external .js file */
    window.onload = function() {
        var img = document.createElement("img");
        img.src = "...";
        img.onclick = wtv;
        img.style.cursor = "pointer"; // so the mouse turns into a finger,
                                      // like on a hyperlink
                                      // Note: instead assign a class attribute and put this in an external CSS file...
        document.getElementById("myButtonParent").appendChild(img);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my web application we have many users.I want to set permission for each
I have similar instances of the same web application running across many domains. I
We have an ASP.NET web application hosted by a web farm of many instances
I am creating a web application that will have many users. Each user has
I want to use bar chart in web application. I have searched many libraries
I have an webapplication that uses devise database authentication for all the controllers, however
I have two webapplication at prrduction say app1 and app2. Both are on different
I have a webapplication using only HTML and Javascript (no server interaction). At this
I have an MVC3 webapplication which runs as a DOMAIN\USER1 account. This user has
I have developed a webapplication in both ASP.NET MVC and ASP.NET Webforms and i'm

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.