HTML DOM insertBefore() मेथड एक नए नोड को पहले से मौजूद चाइल्ड नोड से पहले सम्मिलित करता है।
सिंटैक्स
निम्नलिखित सिंटैक्स है −
positionString और text पैरामीटर्स के साथ insertBefore() कॉल करना
node.insertBefore(newNode, existingNode)
यहां, पैरामीटर्स निम्नलिखित हो सकते हैं −
पैरामीटर्स
| पैरामीटर्स | विवरण |
|---|---|
| newNode | यह नया बनाया गया चाइल्ड नोड है जिसे शुरुआत में जोड़ा जाना है |
| existingNode | यह पहले से मौजूद नोड है |
उदाहरण
आइए 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>चाय कैसे बनाएं</h1> <h3>चरण:</h3> <ol id="stepList"> <li>चाय की पत्ती डालें</li> <li>चीनी डालें</li> <li>दूध डालें</li> </ol> <input type="button" onclick="addStep()" value="जोड़ें"> </fieldset> </form> <script> function addStep() { var newIngredient = document.createElement("LI"); var textnode = document.createTextNode("पानी उबालें"); newIngredient.appendChild(textnode); var stepList = document.getElementById("stepList"); stepList.insertBefore(newIngredient, stepList.childNodes[0]); } </script> </body> </html>
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा −
'जोड़ें' बटन पर क्लिक करने से पहले −

'जोड़ें' बटन पर क्लिक करने के बाद −
