Using JQuery to replace multiple instances of HTML on one page

Posted by Kenneth B on Stack Overflow See other posts from Stack Overflow or by Kenneth B
Published on 2010-03-15T17:44:16Z Indexed on 2010/03/15 18:09 UTC
Read the original article Hit count: 1759

Filed under:
|
|
|

I've made a script which copies the alt attribute of an image and puts it into a <span>. It also formulates the information divided by a hyphen. It works like a charm, but only with one image.

I would also like to wrap a <div> around the image to prevent unnecessary markup.

Script snippets:

HTML:

<div id="hoejre">
    <p>
         <span class="alignright">
             <img src="tommy_stor.jpg" alt="Name - Title" 
                  width="162" height="219" />
             <span></span>
         </span>
   </p>
</div>

jQuery:

var alt = $("#hoejre p span img").attr("alt");
$('#hoejre p span span')
 .html("<strong>" + alt.replace("-", "</strong> <em>") + "</em>");

Output:

<span class="alignright">
    <img height="219" width="162" alt="Name - Title"                  
         src="tommy_stor.jpg">
        <span>
            <strong>Name</strong><em>Title</em>
        </span>
</span>

How do you I repeat the effect on several images, with different information within?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-ui