Question

I'm trying to append a space using jQuery. Neither of these samples work:

  $("#mySelector").append($(" "));
  $("#mySelector").append($(" "));

Any ideas?

Was it helpful?

Solution

How about

$("#mySelector").append(" "); // or with & nbsp;

OTHER TIPS

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(" ");
    }
});

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top