Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Inserting a Button After An Anchor

How in jQuery would I add a button directly after an anchor tag? For example, my HTML looks like:

<div style="border-style: solid; border-width: thin; padding: 2px;">
  <center>
    <a target="_blank" href="http://www.mydomain.com/">MyDomain.com</a>
    <b>My Hypertext Link</b>
    <br/>
    Options:
   <a class="theAClass" href="#">Insert button after this anchor</a>
  </center>
</div>

I want to add a button just after the anchor (class="theAClass") but before the "center" tag so the button is centered on the page.

like image 754
GregH Avatar asked May 21 '26 04:05

GregH


2 Answers

$('.theAClass').after('<button>...</button>')

Should do the trick. That will append the html you specify immediately after the tag.

like image 191
Jamie Macey Avatar answered May 23 '26 17:05

Jamie Macey


//Add button after every theAclass link
$('a.theAClass').after('<button type="button">Click Me!</button>');

//Add button after only the first theAclass link
$('a.theAClass:first').after('<button type="button">Click Me!</button>');
like image 42
micahwittman Avatar answered May 23 '26 16:05

micahwittman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!