If you do not want to use jquery, you should use closeSpan.appendChild and document.createTextNode like this:
var closeSpan = document.createElement("span"); closeSpan.setAttribute("class","sr-only"); closeSpan.appendChild(document.createTextNode("Close"));
This method maximizes cross-browser compatibility. It will work in all browsers, including older versions of IE.
If you do want to use jquery, you can do it on one line:
var closeSpan = $("<span></span>").addClass("sr-only").text("Close")[0];
source share