jQuery Data Type How to - Replacing " " with (space)








Question

We would like to know how to replacing " " with (space).

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!-- w w w . ja v  a2 s .  c o  m-->
    function showSpaces(word) {
        var letter, result = "";
        for (var count = 0; count < word.length; count++) {
            letter = word.charAt(count);
            if (letter == " ") {
                result += "(space)";
            } else {
                result += letter;
            }
        }
        return result;
    }
    document.writeln(showSpaces("Space: the final frontier"));

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: