Get last character of string in jQuery

slice(-1) method in jquery return the last character of string.

var country = 'India';
var lastChar = country.slice(-1);

If you want to more detail about slice, Click Here.

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('.btnGetLastCharacter').click(function (event) {
        var name = $('.txtValue').val();
        var lastChar = name.slice(-1);
        alert(lastChar);
      });
    });
  </script>
</head>
<body>
   <div>
    <input type="text" class="txtValue" value="Nirbhay Singh" /><br /><br />
    <button class="btnGetLastCharacter">Get Last Character</button>
  </div>
</body>
</html>
DISPLAY


No comments :

Please Give Your Feedback