+14 votes
by (100 points)

I have a document with the following structure:

<div id="notice" class="box generalbox">
<p>
This is some text.
</p>
</div>

I want to replace the word "some" with the word "My" using jQuery.

How do i do this?

I tried:

$("#notice").text().replace("some", "My");

But that didnt work...

UPDATE: Thanks for all your replys. I used this solution to get this to work:

$("#notice p").text($("#notice p").text().replace("some", "My"));

12 Answers

+14 votes
by (100 points)

You need to target the p tag inside the #notice :

$("#notice p").text(function(i, text) {
    return text.replace("some", "My");
});

Update 2020-03

This same logic can now be made even simpler by using an arrow function:

$('#notice p').text((i, t) => t.replace('some', 'My'));

This will work in any browser except Internet Explorer.

+8 votes
by (100 points)

Read http://api.jquery.com/text/#text-functionindex--text

$("#notice p").text(function (_, ctx) {
    return ctx.replace("some", "My");
});

or

$("#notice p").text($("#notice p").text().replace("some", "My"));

or

var  p_tag = $("#notice p");
p_tag.text(p_tag.text().replace("some", "My"));
+8 votes
by (100 points)

This is an overkill but anyway:

function replaceNodeText() {
    if (this.nodeType === 3) {
        this.nodeValue = this.nodeValue.replace(replaceNodeText.find, replaceNodeText.replace);
    } else {
        $(this).contents().each(replaceNodeText);
    }
}
replaceNodeText.find = "some";
replaceNodeText.replace = "my";
$("#notice").contents().each(replaceNodeText);

This function will preserve any html present inside the specified element. For example it will work on this HTML:

<div id="notice" class="box generalbox">
    <p>This is<br>some text.</p>
    <p>This is so<br>me text.</p>
    <p>This is <b>some</b> text.</p>
</div>

And produce the following output:

<div id="notice" class="box generalbox">
    <p>This is<br>my text.</p>
    <p>This is so<br>me text.</p>
    <p>This is <b>my</b> text.</p>
</div>

Demo here

+5 votes
by (100 points)
var text = $("#notice p").text()
text = text.replace("some", "My");
$("#notice p").text(text);
0 votes
by (100 points)

Try this, sure you will get your result.

$("#notice p").text(function(i, text) {
    return text.replace("some", "My");
});
0 votes
by (100 points)

Go a head and try this solution:

newtext = $("#notice p").text().replace("some", "My"); 
$("#notice p").text(newtext);
0 votes
by (100 points)

Try this

$('#notice').html().replace("some", "my");
0 votes
by (100 points)

Salmans answer works good, but if you have more than 1 word in a paragraph it wouldn't be replaced. So use this instead: (with a regex that matches globally)

function replaceNodeText() {
    if (this.nodeType === 3) {
        this.nodeValue = this.nodeValue.replace(replaceNodeText.regex, replaceNodeText.replace);
    } else {
        $(this).contents().each(replaceNodeText);
    }
}
replaceNodeText.regex = /some/g;
replaceNodeText.replace = "my";
$("#notice").contents().each(replaceNodeText);

Or use this:

(I just changed .replace(x,y) to .split(x).join(y), this is way faster than replace(), see here )

function replaceNodeText() {
    if (this.nodeType === 3) {
        this.nodeValue = this.nodeValue.split(replaceNodeText.find).join(replaceNodeText.replace);
    } else {
        $(this).contents().each(replaceNodeText);
    }
}
replaceNodeText.find = "some";
replaceNodeText.replace = "my";
$("#notice").contents().each(replaceNodeText);

Demo on jsfiddle

0 votes
by (100 points)

I think you have to pass "x-textrazor-key: YOUR_API_KEY" as additional header

$.ajax({
url: "https://api.textrazor.com/",
type: "POST",
dataType: 'json',
beforeSend: function(xhr){xhr.setRequestHeader('x-textrazor-key', 'YOUR_API_KEY');},
data: { 
   extractors: "entities,entailments",
   text:"Spain's stricken Bankia expects to sell..." 
},
success:function(data) {
    alert(JSON.stringify(data));
},error: function(xhr) {
    alert("<some error>");
    console.error(xhr.responseText);
}});
0 votes
by (100 points)

ACF updated the Google Map documentation

You first have to get a Maps API key and make sure you activate the following APIs :

  • Maps JavaScript API
  • Geocoding API
  • Places API

Then register the API key in your functions.php

If using ACF free

function my_acf_google_map_api( $api ){
    $api['key'] = 'xxx';
    return $api;
}
add_filter('acf/fields/google_map/api', 'my_acf_google_map_api');

If using ACF pro

function my_acf_init() {
    acf_update_setting('google_api_key', 'xxx');
}

add_action('acf/init', 'my_acf_init');

In my case I had to delete & recreate the field so that it saves correctly.

Related questions

+2 votes
10 answers
+8 votes
8 answers
...