2
I have a div
editable where I capture all text within it. Hence I want in each occurrence of certain word this word is replaced by another. Very simple.
The code I tried did not work. It itself works only that the output does not work (which in this case is the input itself). But if it is placed in a alert()
for example, it works.
JS
function teste(){
var campo = document.getElementById('editorid').textContent;
var docrg = /palavra/g; // procurar por 'palavra' sem ignorar case
campo.replace(docrg, "outrapalavra"); // replace 'palavra' por 'outrapalavra'
}
document.getElementById('botao').addEventListener('click',teste,false);
HTML
<div id="editorid" contenteditable="true"></div>
<button id="botao">clica</button>
CSS (optional for edge display only)
#editorid{
width: 500px;
height: 500px;
border: 1px solid black;
padding: 10px;
}
Note: This problem is just a module of a text editor that I’m trying to do.
This is very simple if you only want to do it when you click the big stick. To do on the fly is much more difficult, because of the cursor basically. I was preparing an answer to do in real time when scregve (on the fly). If you just want to press the button say :)
– Sergio
I left an answer with the simple version. I explain better in a little while. This holiday season leaves little free time :)
– Sergio
Failed to assign/update the field value. Already solved.
– ropbla9