content
content

Jquery: add html after group divs

I have something like this: ...

<div id="d120" >content</div>
<div id="d123" >content</div>
<div id="d112" >content</div>
<div id="d145" >content</div>
<div id="d134" >content</div>
//Insert here hello world

<div id="bla" >asd</div>
<div id="footer" >asd</div>

does anyone know how to insert html after all divs having id as d + number

+5
source share
5 answers

If the format has nothing between these divs and #bla, as your example, here is a safer approach using .before()(since it div[id^=d]will match <div id="doodlesticks">).

$("#bla").before("<b>Hi There</b>");

Update: . Since you said you could give them a class, I would do it, so give a div div and use this jQuery:

$(".content:last").after("<b>Hi There</b>");
+3
source

This will safely check for elements whose id ends with a number, and not any id starting with "d".

$('div[id]').filter(function() {
    return /\d+$/.test($(this).attr('id'));
}).last().after(...my html...);
+3
source
$("div[id^=d]:last").after("some html");

$("some html").insertAfter("div[id^=d]:last");
+1

div id, html $('# id'). html ('html');

?

0

:

$(function(){
  $('div[id^="d"]:last').after('<div>Hello World</div>');
});

Note: As Nick Craver suggested, this will match any div whose idstarts with d, but I think you need to resort to your html markup. p>

0
source

All Articles