Create a span element inside another element using javascript

Create a span element inside another element using javascript

This code is from google calender.
var dateString = (startJSDate.getMonth() + 1) + “/” + startJSDate.getDate();
if (!startDateTime.isDateOnly()) {
dateString += ” @ ” + startJSDate.getHours() + “:” +
padNumber(startJSDate.getMinutes());
}
dateString = “” +dateString + ““;
var li = document.createElement(‘li’);

I need to add a span tag around the variable dateString, adding them as above returns “1234” as text on the page.
The string is rendered as such:
li.appendChild(document.createTextNode(‘ – ‘ + dateString));

Solutions/Answers:

Solution 1:

Try following code, create span using createElement and insert date string in it as innerHTML. Then append span to li.

var dateSpan = document.createElement('span')
dateSpan.innerHTML = dateString;
var li = document.createElement('li');
li.appendChild(dateSpan);

Solution 2:

if you can use jquery, then the method .wrap() will do it what you want.

References

Related:  Plugin throwing TypeError after WordPress 4.5 update