0

I have 2 arrays that have the exact same number of keys. I would like to loop through the first array, then search <p> tags for any matching values. Then if a match is found, I would like to replace that matching value with the corresponding value from the 2nd array.

I have this for now:

var text_original = ["original_word1", "original_word2", "original_word3"];
var text_replaced = ["replaced_word1","replaced_word2","replaced_word3"];
var z;

for (z = 0; z <= text_original.length; ++z) {
  $("p").each(function(){
    var text=jQuery(this).html();
    $(this).html(text.replace(text_original,text_replaced));
  });
}

This is the general idea, I'm getting confused how to isolate the single values from the multiple array values. Thanks in advance for help!

8
  • Please post a Minimal, Complete, and Verifiable example Commented Mar 13, 2018 at 19:20
  • Thanks, this is minimal, and I can't produce the code that I don't know how to write in the first place. If I gave everything, it would complicate worse. Commented Mar 13, 2018 at 19:25
  • Please, post the content of the two arrays. Commented Mar 13, 2018 at 19:27
  • Oh ok. Coming right up - thanks! Commented Mar 13, 2018 at 19:27
  • Doing this $("p") for ech iteration is a bad practice. Commented Mar 13, 2018 at 19:29

2 Answers 2

2
  • Use the function indexOf and check for the found text.
  • Use the function text rather than the function html.

var text_original = ["original_word1", "original_word2", "original_word3"];
var text_replaced = ["replaced_word1", "replaced_word2", "replaced_word3"];

$('p').each(function() {
  var text = $(this).text();
  
  var index = text_original.indexOf(text);
  if (index > -1) $(this).text(text.replace(text_original[index], text_replaced[index]));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>original_word1</p>
<p>original_word2</p>
<p>Ele from SO</p>

Why $.text()?

The HTML <p> element represents a paragraph of text. Paragraphs are usually represented in visual media as blocks of text that are separated from adjacent blocks by vertical blank space and/or first-line indentation. Paragraphs are block-level elements.

The function $.text() will return the text without HTML tags.

Sign up to request clarification or add additional context in comments.

Comments

1
  • You missing echo before json_encode().
  • Your json_encode() should not be wrapped into quotes.
  • You have to use [z] to access to an element of an array.

Code :

var text_original = <?php echo json_encode($text_original); ?>;
var text_replaced = <?php echo json_encode($text_replaced); ?>;
var z;
for (z = 0; z <= text_original.length; ++z) {
    $("p").each(function(){
        var text=jQuery(this).html();
        $(this).html(text.replace(text_original[z],text_replaced[z]));
    });
}

2 Comments

Hi @Syscall, I edited the original question to be slightly different. Nevermind the PHP code I originally placed. Sorry for that. Thanks!
Brilliant, and simple. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.