How to remove first character from a string in jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the JavaScript substring() method

You can use the JavaScript substring() method to remove first character form a string. A typical example is removing hash (#) character from fragment identifier.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>jQuery Remove first Character from String</title>
  • <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
  • <script type="text/javascript">
  • $(document).ready(function(){
  •     $("a").click(function(){
  •         var fid = $(this).attr("href");
  •         var itemId = fid.substring(1, fid.length);
  •         alert("The ID of the linked image is - " + itemId);
  •     });
  • });
  • </script>
  • </head>
  • <body>
  •     <a href="#balloons">Show Balloons</a>
  •     <br>
  •     <img src="balloons.jpg" style="margin-top: 1000px" alt="Hot Air Balloons" id="balloons">
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.