jQuery replaceWith() Metode

Metode HTML/CSS jQuery

Contoh

Ganti elemen <p> pertama dengan teks baru:

$("button").click(function(){
   $("p:first").replaceWith("Hello world!");
});

Definisi dan Penggunaan

Metode replaceWith() menggantikan elemen yang dipilih dengan konten baru.


Sintaksis

$(selector).replaceWith(content,function(index))

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

Possible values:

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

Cobalah Sendiri - Contoh


Cara menggunakan fungsi untuk mengganti elemen yang dipilih dengan konten baru.


Metode HTML/CSS jQuery