Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I recommend against using classes if you're not going to associate the class with the image. (<em>which would be the most correct way</em>) What I would do instead is put a link to the image in the rel tag. </p> <p>This does what you want, and will still validate as valid css. </p> <pre><code>&lt;div class="button" rel="images/star.jpg"&gt;iconStar&lt;/div&gt; &lt;div class="button" rel="images/plus.jpg"&gt;iconPlus&lt;/div&gt; &lt;div class="button" rel="images/left.jpg"&gt;iconLeft&lt;/div&gt; &lt;div class="button" rel="images/right.jpg"&gt;iconRight&lt;/div&gt; &lt;div class="button" rel="images/up.jpg"&gt;iconUp&lt;/div&gt; &lt;div class="button" rel="images/down.jpg"&gt;iconDown&lt;/div&gt; &lt;script&gt; $('.button').each(function() { $(this).append("&lt;img src='"+$(this).attr('rel')+"'&gt;"); }); &lt;/script&gt; </code></pre> <p>See the example here: <a href="http://jsbin.com/acasu" rel="nofollow noreferrer">http://jsbin.com/acasu</a></p> <p>Note, if you're using a lot of tiny images, you're going to want to use <a href="http://www.alistapart.com/articles/sprites" rel="nofollow noreferrer">CSS Sprites</a>. As it will greatly improve the performance of your page.</p> <p>If you absolute had to do it the way you are suggesting, you could do the following:</p> <pre><code>$(".button[class^='button icon']").each(function() { var iconSrc = $(this).attr('class').substr("button icon".length) $(this).append("&lt;img src='/images/"+iconSrc+".jpg'&gt;"); }); </code></pre>
 

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