HTML DOM 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> आउटपुट
यह निम्नलिखित आउटपुट देगा -
'जोड़ें' . क्लिक करने से पहले बटन -

‘जोड़ें’ clicking क्लिक करने के बाद बटन -
