Can I insert elements to the beginning of an element using .appendChild()?
Asked Answered
M

6

107

If the user selects an option in a dropdown box, there must be added a label and a textbox. Using appendChild, these elements get added to the end of the container.

var newFreeformLabel = document.createElement('label');
newFreeformLabel.innerHTML = 'Omschrijving:';

var newFreeformField = document.createElement('input');
newFreeformField.className = 'textfield';
newFreeformField.name = 'factuur_orderregel[]';

var newFreeformSpacer = document.createElement('div');
newFreeformSpacer.className = 'spacer';

container.appendChild(newFreeformLabel);
container.appendChild(newFreeformField);
container.appendChild(newFreeformSpacer);

The issue is that these elements should be inserted at the beginning of container, not at the end.

Is there a solution to this in PrototypeJS?

Megavolt answered 6/3, 2009 at 8:47 Comment(0)
B
190

As well as appendChild, DOM nodes have an insertBefore method

container.insertBefore(newFreeformLabel, container.firstChild);
Blastomere answered 6/3, 2009 at 9:30 Comment(1)
Note that this also works if the container doesn't have any elements inside it to begin with.Whidah
B
127

Modern Solution

To add a child to the beginning of a parent, use prepend

parent.prepend(newChild)

To add at the end of a parent, use append

parent.append(newChild)

In addition, if you want to add relative to another child, use one of these:

child1.after(newChild)  // [child1, newChild, child2]

child1.before(newChild) // [newChild, child1, child2]

Advanced usage

  1. You can pass multiple values (or use spread operator ...).
  2. Any string value will be added as a text element.

Examples:

parent.prepend(newChild, "foo")  // [newChild, "foo", child1, child2]

const list = ["bar", newChild]
parent.append(...list, "fizz")  // [child1, child2, "bar", newChild, "fizz"]

Related DOM method - child.replaceWith

Documentation

Can I Use

Boiney answered 13/8, 2017 at 3:2 Comment(1)
this answer is working, the first answer not worked for meWeathered
L
11

Use Element.insert(element, content).

Looming answered 6/3, 2009 at 8:50 Comment(1)
the question is tagged prototypejs, so this really should be the correct answer.Laniferous
C
1
container.
    insert({
        // key is position
        // 'before', 'after', 'top' and 'bottom' are allowed
        top: new Element('label').
            update('Omschrijving:')
    }).
    insert({
        top: new Element('input').
            addClassName('textfield').
            writeAttribute('name', 'factuur_orderregel[]')
    }).
    insert({
        top: new Element('div').
            addClassName('spacer')
    });

I think Prototype's Element.insert is somewhat awkward for before/after, however. For instance, if you wanted to place the .spacer before the .textfield, sometime later in your code, you would need to do something like:

container.
    down('.textfield').
    insert({
        before: new Element('div').
            addClassName('spacer')
    });

This is, especially if you're familiar with the DOM API's Element.insertBefore, somewhat unintuitive, as you are not inserting the .spacer into the .textfield, but instead into the container, before the .textfield.

Canvass answered 6/3, 2009 at 9:12 Comment(0)
A
0

If I had seen Gareth's solution on time, I might go with that, as it was I used:

$('toggle_product').innerHTML = insertContent + $('toggle_product').innerHTML;

as Prototypes' insert() function was returning error in IE8..

Arron answered 7/5, 2012 at 11:46 Comment(1)
with that solution you loose all eventsNorthward
D
0

You can add .k-multiselect .k-chip {order: 2} style. Example - https://dojo.telerik.com/uSOnIroB/4

Dialogue answered 14/3, 2023 at 14:0 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sugihara
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewPresbyopia

© 2022 - 2024 — McMap. All rights reserved.