Replace HTML entities within a Div

Asked

Viewed 364 times

1

I have a code html and I need to replace part of it with another html, this code is inside a div.

Code:

<div id="aba-per" class="et_pb_module et_pb_tabs tab-per et_pb_tabs_0">
    <ul class="et_pb_tabs_controls clearfix">
        <li class="et_pb_tab_0 et_pb_tab_active"><a href="#">Teste &rarr; Teste</a></li>
    </ul>
</div>

I want to replace &rarr; for <br>, that is, instead of showing the arrow I need the line to be broken.

  • Does this appear only on this site or in more places? Is this HTML generated on the server? can’t change there?

  • @Sergio on your watch is my answer duplicated? : P If I turn into wiki and vote to close :)

  • 1

    @Marcelobonifazio I think the question is not clear enough. I am also waiting for clarifications.

  • 1

    It lacked a why, what the need, indeed... but I thought it was possible to answer according to what was asked, although it would make more sense to wait, since &rarr; only makes sense on the server side, from the moment the browser renders has no reason to use javascript

  • I need this to be done due to a system that is already ready in PHP, I need to modify a tab (tab) inside it, the control panel has the option to modify, but accepts only 1 line, I want to put the title in 2 lines.

Show 1 more comment

1 answer

1

It turns out that &rarr; in the browser will be rendered with its corresponding character, in case

What you can do is take the content using innerHTML, make the change using replace and assign the new text to the div element

function teste() {
  var texto = document.getElementById("aba-per").innerHTML;
  texto = texto.replace("→", "<br/>");
  document.getElementById("aba-per").innerHTML = texto;
}
<div id="aba-per" class="et_pb_module et_pb_tabs tab-per et_pb_tabs_0">
  <ul class="et_pb_tabs_controls clearfix">
    <li class="et_pb_tab_0 et_pb_tab_active"><a href="#">Teste &rarr; Teste</a></li>
  </ul>
</div>
<button onclick="teste()">teste</button>

Anyway it seems to me kind of duplicated of that question here: Replace symbols < /> with Javascript see the @Sergio response

Browser other questions tagged

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