tags:

views:

27

answers:

2

I have something like this:

<div id='d12'>content</div>
<div id='d23'>content</div>
<div id='d34'>content</div>

and I would like to insert a link after each link like this

<div id='d12'>content</div><a href='javascript:add(d12)'/>add</a>
+1  A: 
$('div').each(function() {
    var id = $(this).attr('id');
    if (id.match(/^d\d+$/)) {
        $('<a />')
            .text('add')
            .attr('href', '#')
            .click(function() {
                add(id);
                return false;
            })
            .insertAfter(this);
    }
});
reko_t
Slightly more elegant with `$('div[id^=d]')`.
Tgr
+2  A: 
$('div[id^=d]').each(function(){
  $(this).after(
    "<a href='javascript:add(" + $(this).attr("id") + ")'>add</a>"
  );
});
Salman A