To begin my code snippet articles for small functions that I have written. Wonder how you can write Ngo’mbe using Javascript. You use the single quote.

Example:

var TheText;
TheText = ‘Ngo\’mbe’;

Explanation

The backslash tells JavaScript (this has nothing to do with jQuery, by the way) that the next character should be interpreted as “special”. In this case, an apostrophe after a backslash means to use a literal apostrophe but not to end the string.
There are also other characters you can put after a backslash to indicate other special characters. For example, you can use \n for a new line, or \t for a tab.

Demo

Inside an HTML it would look something like this:
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta charset=”utf-8″ />
</head>
<body>
  <label>Select an animal to translate to Kiswahili</label>
  <br />
  <select id=”Animal” name=”Animal” title=”Animal” onchange=”showAnimal(this.options[this.selectedIndex].value)”>
  <option selected=”selected” value=”cat”>Cat</option>
  <option value=”cow”>Cow</option>
  <option value=”dog”>Dog</option>
  </select>
  <div id=”show_animal_here”></div>
  <script type=”text/javascript”>
  function showAnimal(name) {
  if (name == ‘cow’) { document.getElementById(‘show_animal_here’).innerHTML = ‘<label>Ngo\’mbe</label>’; }
  else if (name == ‘dog’) { document.getElementById(‘show_animal_here’).innerHTML = ‘<label>Mbwa</label>’; }
  else if (name == ‘cat’) { document.getElementById(‘show_animal_here’).innerHTML = ‘<label>Paka</label>’; }
  else { document.getElementById(‘show_animal_here’).innerHTML = ”; }
  }
  </script>
</body>
</html>

Leave a comment

Your email address will not be published. Required fields are marked *