user3800924
user3800924

Reputation: 407

Create two new elements and append one after in javascript

var span = document.createElement('span');
var text = document.createTextNode('my text');

// I know this does not work but you know what I mean:
    var element = span + text;
    parentElement.appendChild(element);

    or with jQuert

    $('#aDiv').append(element);

How do I add the text node AFTER the span? and then I just add the span with the text to another element that exists.

Upvotes: 1

Views: 53

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

Append text after span, like so

parentElement.appendChild(span);
parentElement.appendChild(text);

Or with jQuery

$(parentElement).append(span);
$(parentElement).append(text);

Example

Upvotes: 3

Related Questions