jQuery prepend() Metode

Metode HTML/CSS jQuery

Contoh

Sisipkan konten di awal semua elemen <p>:

$("button").click(function(){
  $("p").prepend("<b>Prepended text</b>");
});

Definisi dan Penggunaan

Metode prepend() menyisipkan konten tertentu di awal elemen yang dipilih.

Tip: Untuk menyisipkan konten di akhir elemen yang dipilih, gunakan metode append() .


Sintaksis

$(selector).prepend(content,function(index,html))

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

Possible values:

  • HTML elements
  • jQuery objects
  • DOM elements
function(index,html) Optional. Specifies a function that returns the content to insert
  • index - Returns the index position of the element in the set
  • html - Returns the current HTML of the selected element

Cobalah Sendiri - Contoh


Sisipkan konten dengan metode prepend().


Menggunakan fungsi untuk menyisipkan konten di awal elemen yang dipilih.


Metode HTML/CSS jQuery