Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insertAdjacentHtml in jquery

for some reason when using insertAdjacentHtml function in FF I am getting insertAdjacentHTMl is not a function error, is there an alternative to this in jQuery or some other alternative javascript function?

like image 589
anon Avatar asked Jul 07 '11 22:07

anon


1 Answers

It depends on how you're using it.

.insertAdjacentHTML("beforeBegin", ...) //$('...').before(...)
.insertAdjacentHTML("afterBegin", ...) //$('...').prepend(...)
.insertAdjacentHTML("beforeEnd", ...) //$('...').append(...)
.insertAdjacentHTML("afterEnd", ...) //$('...').after(...)

http://api.jquery.com/before/

http://api.jquery.com/after/

http://api.jquery.com/append/

http://api.jquery.com/prepend/


Code Example

$('<p class="border">PrependTo</p>').prependTo($('.main'));
    $('.main').prepend('<p class="border">Prepend</p>');

    $('<p class="border">AppendTo</p>').appendTo($('.main'));
$('.main').append('<p class="border">Append</p>');


$('<p class="border">Insert After</p>').insertAfter('.main');

$('<p class="border">Insert Before</p>').insertBefore('.main');
.border {
  border: 1px solid #000;
  margin: 10px;
  padding: 5px 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main border">
  <p>Main</p>
</div>
like image 53
Adam Terlson Avatar answered Oct 12 '22 11:10

Adam Terlson