Last Updated on August 7, 2022 by Roshan Parihar
In this tutorial, learn how to remove character from the string on the button click with jQuery. The short answer is to 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.
Method 1: jQuery Remove Character From String Using replace() with ‘/g’
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 named ‘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.
The /g
in the replace function scans the mentioned character from all the letters of the string. It means global characters in a string.
1 2 3 4 5 6 7 8 9 10 11 12 |
<script> $(document).ready(function(){ $('#btnRemove').click(function(){ var myText = $('.MyString').text(); var myRemoveChar = myText.replace(/s/g,''); $('.removedString').html('<strong>The new string is: </strong>'+myRemoveChar); }); }); </script> <p class="MyString">This is my string</p> <button type="button" id="btnRemove">Click me to Remove Character s</button> <p class="removedString"></p> |
Output
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’.
Method 2: Delete Character From String Using replace() and RegExp()
If you delete characters from a string, you can use the replace()
inside which you have to use the RegExp()
as the first argument. The second argument is space within quotes (‘ ‘).
Also, specify the first argument of the RegExp()
function as the character to delete from the string. The second argument of the RegExp()
function is ‘g’ to scan globally all the characters of the string.
1 2 3 4 5 6 7 8 9 10 11 12 |
<script> $(document).ready(function(){ $('#btnDelete').click(function(){ var myTxt = $('.MyStr').text(); var myDeleteChar = myTxt.replace(RegExp('s','g'),''); $('.DeleteString').html('<strong>The new string is: </strong>'+DeleteString); }); }); </script> <p class="MyStr">This is my string</p> <button type="button" id="btnDelete">Click me to Remove Character s</button> <p class="DeleteString"></p> |
Output
This is my string
When you click the above button, you will get the new string without character ‘s’. It is simple to use without the need to use slashes (/
) given in the previous example.
Method 3: Remove Single Letter From the String Using split() and join()
In addition to the above example, you can also remove the character from the string using the split()
and join()
function. You have to pass the character as the argument of the split()
function.
1 2 3 4 5 6 7 8 9 10 11 12 |
<script> $(document).ready(function(){ $('#btnRm').click(function(){ var Txt = $('.Str').text(); var Rmletter = Txt.split('i').join(''); $('.resultStr').html('<strong>The new string is: </strong>'+Rmletter); }); }); </script> <p class="Str">This is my string</p> <button type="button" id="btnRm">Click to Remove Character i</button> <p class="resultStr"></p> |
Output
This is my string
You have to click the above button to remove the character ‘i’ from the string. This is a useful and simple example to removing all the specified letters from the text.
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
I hope you like this post on how to remove character from string using jQuery.
Reference