AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



jquery-html: How to replace a word by another word by jquery?

I have one paragraph tag and i want to replace one word by another word by jquery.

jquery x 12
html x 24
Posted On : 2015-12-23 00:33:15.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250050
up-rate
4
down-rate

Answers


you can use replace(´oldchars´,´newchars´) method in jquery. First get the text of that paragraph tag and replace the respective string and then set back the new text into it. If you want to replace only one word with only one occurrence in the text you can simply call replace("Old Word","New Word"). and if there is more than one occurrences or you want to replace all the words by new one then use regular expression in place of old word in replace method. See the below code snippet:

replace.html:
-----------------------------------------
<html>
<head>
<title>Replace in jquery-anyforum.in</title>
</head>
<body>
<p id="content">Hi, How are you? Hi, I am fine.</p>
<button id="button">Click here to replace</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(´button´).click(function(){
//$(´#content´).text($(´#content´).text().replace(´Hi´,´Bye´));//replaces only first occurrence
$(´#content´).text($(´#content´).text().replace(/\bHi\b/gi,´Bye´));
});
</script>
</body>
</html>
Output:
------------------------------

Hi, How are you? Hi, I am fine.


Posted On : 2015-12-23 00:48:57
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in