swapNode method
Interchanges the positions of the current element and the specified element in the document hierarchy.
For a cross-browser solution, use the replaceChild, insertBefore and appendChild methods instead.
See the example below.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. Reference to the element to swap with. |
Return value:
Returns a reference to the current element.
Example HTML code 1:
This example illustrates the use of the swapNode method:
|
||||
<head> <script type="text/javascript"> function SwapWithFirst (button) { var container = document.getElementById ("container"); var firstButton = container.getElementsByTagName ("button")[0]; if (button != firstButton) { if (button.swapNode) { button.swapNode (firstButton); } else { var next = button.nextSibling; container.replaceChild (button, firstButton); container.insertBefore (firstButton, next); } } } </script> </head> <body> <div id="container"> <button onclick="SwapWithFirst (this)" style="color:red">Swap me with the first</button> <button onclick="SwapWithFirst (this)" style="color:green">Swap me with the first</button> <button onclick="SwapWithFirst (this)" style="color:blue">Swap me with the first</button> </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CommentNode, TextNode
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:reset, input:submit, input:text, ins, isindex, kbd, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments