If you are finding out the easy way to replace character of a string, then use this replaceAll() jQuery inbuilt method. You may use the .replaceAll() method to find and replace all occurrences of a word inside any string. Let us show you the better example along with the syntax, how to find and replace a string with another in jQuery.

Syntax

$(content).replaceAll(selector)

replaceAll() method has 2 required parameter, as mentioned above.

Example Code

Replace all <p> elements with <h3> elements

<script>

            $(document).ready(function() {

                $(“button”).click(function() {

                    $(“<h3>UsejQuery</h3>”).replaceAll(“p”);

                });

            });

</script>

Full Program

This program will show how the above function works. Let’s enter into the example and learn to try it out.

<!DOCTYPE html> 
<html> 
	<head> 
		<title>The replaceAll Method</title> 
		<script src= 		"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
		</script> 
		<script> 
			$(document).ready(function() { 
				$("button").click(function() { 
					$("<h3>UsejQuery</h3>").replaceAll("p");
				}); 
			}); 
		</script> 
	</head> 
	<body> 
		<div> 
			<p>Welcome</p>
			<button>Click to Replace</button> 
			<br> 
		</div> 
	</body> 
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *