Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String replacing in a div

I want to replace a particular string in #TextArea1. This happens when a button is clicked.

Trying it out with the below code, but unable to get it work:

$('#TextArea1').text().replace("wef","--");

What is the correct syntax to replace a word in a div?

like image 427
X10nD Avatar asked Sep 17 '11 18:09

X10nD


People also ask

How do I change the inner text of a div?

Set the innerHTML Property of an Element querySelector('div') div. innerHTML = "My new text!"; to select the div with querySelector . And then we can set the innerHTML to a new string.

How do I change the content of a div?

Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content. Here is the HTML for the examples in this article.

Does innerHTML replace text?

The Element property innerHTML gets or sets the HTML or XML markup contained within the element. To insert the HTML into the document rather than replace the contents of an element, use the method insertAdjacentHTML() .

How do you replace data in a string?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced.


4 Answers

Pass a function to the text()[docs] method that returns the value you want set:

$('#TextArea1').text(function( i, txt ) { 
    return txt.replace("wef","--"); 
});

The function parameters are as follows:

  • i is the index of the current element in the iteration
  • txt is the current text content of the current element

The value returned from the function will be set as the new value for the current element.

like image 105
user113716 Avatar answered Oct 16 '22 09:10

user113716


You are close, try this:

$('#TextArea1').text($('#TextArea1').text().replace(/wef/g,"--"));

Or an optimized one

var $textarea = $('#TextArea1');
$textarea.text($textarea.text().replace(/wef/g,"--"));

If it's a textarea element, you would do:

var $textarea = $('#TextArea1');
$textarea.val($textarea.val().replace(/wef/g,"--"));
like image 39
Shef Avatar answered Oct 16 '22 10:10

Shef


You have set the text also:

var text = $('#TextArea1').text().replace("wef","--");
$('#TextArea1').text(text);

or, using a function:

$('#TextArea1').text(function(index, text) {
  return text.replace("wef","--");
});

Note: if this is a <textarea>, use val() instead of text().

var text = $('#TextArea1').val().replace("wef","--");
$('#TextArea1').val(text);
like image 22
Digital Plane Avatar answered Oct 16 '22 09:10

Digital Plane


replace() creates a new string and returns it, so it's getting returned into thin air. You need to grab the new string and send it back into the textbox. This jsfiddle shows how.

<textarea id="t">
    hello
</textarea>
var text = $('#t').text();
text = text.replace('h', 'b');
$('#t').text(text);
like image 21
Matt Greer Avatar answered Oct 16 '22 09:10

Matt Greer