jQuery insertBefore() Metode

Metode HTML/CSS jQuery

Contoh

Masukkan elemen <span> sebelum setiap elemen <p>:

$("button").click(function(){
  $("<span>Hello world!</span>").insertBefore("p");
});

Definisi dan Penggunaan

Metode insertBefore() menyisipkan elemen HTML sebelum elemen yang dipilih.

Tip: Untuk menyisipkan elemen HTML setelah elemen yang dipilih, gunakan metode insertAfter() .


Sintaksis

$(content).insertBefore(selector)

Parameter Description
content Required. Specifies the content to insert (must contain HTML tags).

Note: If content is an existing element, it will be moved from its current position, and inserted before the selected elements.
selector Required. Specifies where to insert the content

Cobalah Sendiri - Contoh


Cara menggunakan metode insertBefore() untuk menyisipkan elemen yang ada sebelum setiap elemen yang dipilih.


Metode HTML/CSS jQuery