jQuery Page Widget How to - Get first letter








Question

We would like to know how to get first letter.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>
$(function(){<!--from  www  .j a v  a2s  .c  o m-->
    var x = $('.formatted-list li:first-child').text().substr(0,1); //':first-letter'
    console.log(x);
});
</script>
</head>
<body>
  <ul class="formatted-list">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
  </ul>
</body>
</html>

The code above is rendered as follows: