Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Short Answer</h2> <p>Use an <code>&lt;img&gt;</code> - not a button or an anchor or an input - as the rest suggest that the element is interactive, <em>even without JavaScript</em>.</p> <h2>Long Answer</h2> <blockquote> <p>clicking on the image is supposed to call an action (via javascript, but that's not the point).</p> </blockquote> <p>I disagree; that <em>is</em> the point :)</p> <p>Because the clicking activates JS-only features, your image should only be available in a JS environment.</p> <p>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.</p> <h2>The Wrong Way</h2> <pre><code>&lt;div&gt; Click on the image to do something: &lt;a href="javascript:wtv()" style="background-image:url(...)"&gt;&amp;nbsp;&lt;/a&gt; &lt;/div&gt; &lt;div&gt; Click on the image to do something: &lt;input type="image" onclick="wtv()" src="..." /&gt; &lt;/div&gt; &lt;div&gt; Click on the image to do something: &lt;img onclick="wtv()" src="..." /&gt; &lt;/div&gt; &lt;div&gt; Click on the image to do something: &lt;button onclick="wtv()"&gt;&lt;img onclick="wtv()" src="..." /&gt;&lt;/button&gt; &lt;/div&gt; </code></pre> <p>These are all wrong because a user who doesn't have JavaScript sees these items and can't use them.</p> <p>Of all of these, I'd say the <code>&lt;img&gt;</code> is the lesser evil, as it doesn't suggest an interactive element. The greatest evil is using the <code>&lt;a&gt;</code> as an anchor should be a hyperlink to another document, and you should never, <em>ever</em> use the <code>javascript:</code> protocol.</p> <p>You'll still have the same problem when you add the JavaScript event handlers externally:</p> <pre><code>/* external .js file */ document.getElementById("myButton").onclick = wtv; &lt;!-- HTML document --&gt; &lt;div id="myButtonParent"&gt; Click on the image to do something: &lt;a id="myButton" href="#" style="background-image:url(...)"&gt;&amp;nbsp;&lt;/a&gt; &lt;/div&gt; </code></pre> <p>As, again, you still have the (non-functioning) hyperlink available to those users who don't have JavaScript.</p> <h2>Instead</h2> <p>Instead, insert the whole damn thing using DOM scripting! I'm going to use an <code>&lt;img&gt;</code> with an <code>onclick</code> event:</p> <pre><code>/* 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload