คำสั่งนี้จะทำงานตรงกันข้ามกับคำสั่ง append() โดยมันจะเพิ่มเนื้อหาเข้าไปในส่วนต้น ของเนื้อหาเดิม

prepend(content)

content :(string) เนื้อหาที่ต้องการเพิ่มเข้าไปในส่วนต้นของเนื้อหาเดิม สามารถใช้แท็ก html ปนได้

ตัวอย่างการใช้งาน

Live Demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>jquery-prepend-command</title>
<script type="text/javascript">
$(document).ready(function(){
 
	$('#set').click(function(){
 
		var content = 'ข้อความใหม่ จะเพิ่มเข้าไปส่วนต้น ของเนื้อหาเดิม <i>เน้นๆ</i> ';
 
		$('#content').prepend( content );
 
	});
 
})
</script>
</head>
<body>
 
    <div id="content"><strong>ข้อความเดิม</strong></div>
 
      <input name="set" type="button" id="set"  value="Prepend Text" /> 
 
</body>
</html>

ข้อดีข้อหนึ่งของ jquery คือ ทำเรื่องยากให้เป็นเรื่องง่าย ครับ