45

This code is from google calender.

var dateString =  (startJSDate.getMonth() + 1) + "/" + startJSDate.getDate();
  if (!startDateTime.isDateOnly()) {
  dateString += " @ " + startJSDate.getHours() + ":" + 
  padNumber(startJSDate.getMinutes());
}
 dateString = "<span>" +dateString + "</span>";
 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));
Nicekiwi
  • 4,567
  • 11
  • 49
  • 88

2 Answers2

79

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);
Naren Sisodiya
  • 7,158
  • 2
  • 24
  • 35
1

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

Reporter
  • 3,897
  • 5
  • 33
  • 47