I'm trying to append a space using jQuery. Neither of these samples work:
$("#mySelector").append($(" "));
$("#mySelector").append($(" "));
Any ideas?
5 Answers
How about
$("#mySelector").append(" "); // or with & nbsp;
In my case I did the following:
$('.colwid10a').each(function () {
if ($(this).is(':empty')) {
$(this).append(" ");
}
});
$('.colwid12').each(function () {
if ($(this).find('a').is(':empty')) {
$(this).find('a').append(" ");
}
});
You could also use '\xa0', it is a non break space character.
$("#mySelector").append('\xa0');
And, create a JQuery Plugin function to reuse it whenever you need to put space. This way you will be consistent throughout.
if(!$.space) {
$.space = function(noOfSpaces) {
var space = " ", returnValue = "";
for(var index=0; index < noOfSpaces; index++) {
returnValue += space;
}
return returnValue;
}
}
alert("Stack" + $.space(6) + "Overflow");
Untested (and probably a bit overkill):
$("").append($("<p> </p>").text());