HTML DOM insertBefore() পদ্ধতিটি ইতিমধ্যে বিদ্যমান চাইল্ড নোডের আগে একটি নতুন নোড সন্নিবেশ করায়।
সিনট্যাক্স
নিম্নলিখিত সিনট্যাক্স −
পজিশনস্ট্রিং এবং টেক্সট
এর প্যারামিটার সহ insertBefore() কল করা হচ্ছেnode.insertBefore(newNode, existingNode)
এখানে, প্যারামিটার নিম্নলিখিত −
হতে পারেপরামিতি
| প্যারামিটার | বিবরণ |
|---|---|
| newNode | এটি নতুন তৈরি করা চাইল্ড নোড যা শুরুতে যুক্ত করতে হবে |
| বিদ্যমান নোড | এটি ইতিমধ্যেই বিদ্যমান নোড |
উদাহরণ
আসুন InsertBefore()-এর উদাহরণ দেখি পদ্ধতি -
<!DOCTYPE html>
<html>
<head>
<title>insertBefore()</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
ol {
width:30%;
margin: 0 auto;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>insertBefore( )</legend>
<h1>How to make tea</h1>
<h3>Steps:</h3>
<ol id="stepList">
<li>Add Tea Bag</li>
<li>Add Sugar</li>
<li>Add Milk</li>
</ol>
<input type="button" onclick="addStep()" value="Add">
</fieldset>
</form>
<script>
function addStep() {
var newIngredient = document.createElement("LI");
var textnode = document.createTextNode("Boil Water");
newIngredient.appendChild(textnode);
var stepList = document.getElementById("stepList");
stepList.insertBefore(newIngredient, stepList.childNodes[0]);
}
</script>
</body>
</html> আউটপুট
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে'যোগ করুন' ক্লিক করার আগে৷ বোতাম -

'যোগ করুন' ক্লিক করার পরে৷ বোতাম -
