How to put a DIV in place of a specific word with script?

Asked

Viewed 46 times

-2

I want you where you are text take for example <div> novo texto </div>, I was wondering if you could do this.

In short, I want everywhere on the site to have the floor text change to a DIV and summarize the previous text.

UPDATE

I was able to do what I wanted with that code

$("p:contains('Esgotado')").replaceWith("<div id='esgotado'>SEM ESTOQUE</div>");

With this everywhere on the site that has the word "sold out" inside a tag <p> Esgotado </p> mute to <div id='esgotado'>SEM ESTOQUE</div>

  • 1

    texto_html.replace('texto', '<div> novo texto </div>');

  • gave the error in the console (texto_html is not defined), I changed the name I want to replace that is EXHAUSTED and still gave error.

  • You can’t put one <div> within a paragraph goes against the recommendations of W3C. Even if the browser today supports this behavior nothing guarantees that in a future change this behavior is abolished and starts generating syntax errors. Test in validator of W3C and here a recommence on phrased content.

1 answer

-1

You should use the replaceWith method instead of html.

See if this helps you:

$('p').replaceWith( "<div>" + $('p').text() + "</div>" );
  • I switched here and it worked - $("p:contains('sold out')"). replaceWith("<div id='sold out'>NO STOCK</div>");

  • Oops! Sorry! I forgot to put the ":contains('sold out')". Hug

Browser other questions tagged

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