जावास्क्रिप्ट insertBefore() . प्रदान किया है दूसरे बच्चे के सामने एक बच्चे के रूप में एक नोड सम्मिलित करने की विधि। यदि 2 सूचियां हैं तो हम insertBefore() विधि का उपयोग करके अपनी आवश्यकता के आधार पर उनके बीच के तत्वों को फेरबदल कर सकते हैं ।
वाक्यविन्यास
node.insertBefore(newnode, existingnode);
उदाहरण-1
निम्न उदाहरण में, दो सूचियां हैं और हमारी आवश्यकता के आधार पर सूचियों को insertBefore() का उपयोग करके फेरबदल किया गया था। विधि <मजबूत>। . तक पहुंचने के लिए एक सूची में कई तत्व हम उनकी अनुक्रमणिका का उपयोग कर सकते हैं।
<html>
<body>
<ul id="List1"> <li>Tesla </li><li>Solarcity </li> </ul>
<ul id="List2"> <li>Google </li> <li>Drupal </li> </ul>
<script>
var node = document.getElementById("List2").firstChild;
var list = document.getElementById("List1");
list.insertBefore(node, list.childNodes[1]);
</script>
</body>
</html> आउटपुट
Tesla Google Solarcity Drupal
उदाहरण-2
<html>
<body>
<ul id="List1"> <li>Tesla </li> <li>Solarcity </li> </ul>
<ul id="List2"> <li>Google </li> <li>Drupal </li> </ul>
<script>
var node = document.getElementById("List2").firstChild;
var list = document.getElementById("List1");
list.insertBefore(node, list.childNodes[0]);
</script>
</body>
</html> आउटपुट
Google Tesla Solarcity Drupal