1
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.

1 Answer 1

3

Append text after span, like so

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

Or with jQuery

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

Example

Sign up to request clarification or add additional context in comments.

3 Comments

Or if your code structure forces you to have the text element inserted first (don't really see a use case so just saying) , you could use parentElement.insertBefore(span, text);
Thank you Alexander, but is this the only way to do it? Let say I have created 10 elements do I have to use appendChild() 10 times then?
@user3800924 in our case - answer yes, or use innerHTML like so jsbin.com/fubere/2/edit?html,js,output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.