Jquery – Append selected items in spans

Posted by criley on Stack Overflow See other posts from Stack Overflow or by criley
Published on 2010-04-12T07:29:55Z Indexed on 2010/04/12 7:33 UTC
Read the original article Hit count: 171

Filed under:
|

Hi. I'm trying to select all elements that have a class, get their ID's (if they have one), and append individual spans to the end of my document based on that information. This is as far as I've managed to get:

var element = $(".foo"),
    element_id = element.attr("id"),
    span_id = "for-" + element + "-id-" + element_id,   
    ;

I am hoping to append a span for each element gathered. Here are some examples of the span format I wish to achieve:

<span id="for-img-id-profile"></span>
<span id="for-div-id-content"></span>

If possible, I would also like to allow for elements without an ID to be formatted like so:

<span id="for-h1"></span>

I really appreciate the help as I'm very new to jQuery and am trying to learn.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about append