How to use Replace in Jquery?


In this article, I'm going to explain how to replace a word in a string using Jquery.

In this article, I'm going to explain how to replace a word in a string using Jquery. To Demonstrate that, Let us read text from a div control , replace it with out text and then assign the replaced text to same control, as shown in the below example.This code will replace first occurrence of the string.


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" src="jquery-1.2.6.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#Button1").click(function()
{
var txt = $('#pid').text().replace('','');
alert(txt);
});



});
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<p id="pid">15 <span></span></p>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: