Jquery - How to get text inside <p> and change

Asked

Viewed 82 times

0

I want to change the text inside the <p> but without Jquery query class or ID only the text. I tried to do something with this but it consults the class on <p>

$('p.text').contents().filter(function() {
    return this.nodeType == 3
}).each(function(){
    this.textContent = this.textContent.replace($.trim($('p.text').text()),'Forte Max');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <p class="text">Forte '.$N.'</p>

How do I use the <p>Forte '.$N.'</p> the final number is Forte 15 Then I would like Jquery to identify Text and switch to Forte Max

  • Where it comes from $N? It was not easier to change the logic that assigns the value or print of this?

  • Yes I’ve tried it but it didn’t work, the $N is the numbering of each level assigned to the item. For example I could go to Fort 15 and change to Max but it’s with numbering so I wanted to do it another way.

2 answers

1


You want to replace the text Forte 15 for Forte Max within a paragraph <p> any, right?

The following code will do this. But pay attention, because if the string you want to modify appears in several different paragraphs, in all elements <p> will make this change in the first occurrence of this string.

$("p").text(function(index, modificador) {
    return modificador.replace("Forte 15", "Forte Max");
});

Now, if you want the string to be replaced in all elements <p> page and in all occurrences within that same element (other than the code I described earlier, which will only replace the first occurrence of each paragraph), then adopt the following solution::

$("p").text(function(index, modificador) {
    return modificador.replace(/Forte 15/g, "Forte Max");
});

Note that here the syntax changes a little and the "old string" appears between bars. That’s right. Then comes a letter g.

This g means "global".

  • There’s only one string with the description of Fort 15 so she’ll change a paragraph anyway. If you put it by class as well and identify the text maybe it would help anyone who wanted to identify the text only in that class. Well many thanks Best Reply!

0

The textContent property of the Node interface represents the text content of a node and its descendants. textContent returns all elements of a node. On the other hand, innerText is aware of styling and does not return "hidden text elements".

    let text = document.getElementById('alteracao').textContent;
    console.log(text); // Agora a variável de texto é: 'Forte '.$N.'
    let novotext = document.getElementById('alteracao').textContent = 'Forte Max';
    console.log(novotext);  // O HTML de text agora é: <p class="text" id="alteracao">Forte Max</p>      
<p class="text" id="alteracao">Forte '.$N.'</p>

  • Thanks for trying to help, but let me be clear. $N identifies the number within SQL, ie the final number will be "Strong 15". In the inspecting HTML will be <p>Forte 15</p>, so I want him to identify the text inside <p> that is <p>Forte 15</p> and changes to <p>Forte Max</p>.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.