In this tutorial, learn how to remove single character from the string on button click with jQuery. The short answer is use
replace() of jQuery to remove any single character from the string.
You need to specify the letter which you want to remove from the string. You need to use the click event of jQuery to remove the string on button click.
How to Remove Single Character From String Using jQuery
To perform this task, you have to use the jQuery text() to get the string characters. Use a variable where you want to get the string characters.
Now, suppose you want to remove the character ‘s’ from the string. You have to use the jQuery
replace(). The first argument of the replace function requires the single string name which you want to remove.
/g in the replace function scans the mentioned character from the all the letters of the string.
var myText = $('.MyString').text();
var myRemoveChar = myText.replace(/s/g,'');
$('.removedString').html('<strong>The new string is: </strong>'+myRemoveChar);
<p class="MyString">This is my string</p>
<button type="button" id="btnRemove" class="btn btn-primary">Remove Single Character</button>
This is my string
The above example contains the string and the button. Click the above button to remove the single character ‘s’ from the string. You will get a new string below the button without any character ‘s’.
You may also like to read.
- Remove All White Spaces from a string using jQuery
- Remove special characters from a string using PHP
- How to remove string spaces and dash using jQuery
Hope, you like this tutorial on how to remove single character from the string with jQuery. If you have any query regarding the tutorial, please comment below.
Also tell me, which method you are using to remove the single letter from a string using jQuery.