Reputation: 38
(Edited version of my previous question) So i need to replace a word in a sentence, which i have figured out how to do by now. But it's not working in a form.
I'm giving the user 2 forms, 1 to input a word they want to replace in the sentence "Dit document is een lang document maar ook een simpel document". And another 1 to input a word they want to replace that word with.
The only error it's giving me, is that "woord" and "nieuwewoord" in line 9 are undefined. Which are references to the form.
<form vervangen='form'>
<input type="text" name="woord" placeholder="Wat moet er weg?">
<input type="text" name="nieuwewoord" placeholder="Wat moet er staan?">
<input type="button" value="Vervang"
onclick="document.write(tekstvr)">
<script>
var tekst =
"Dit document is een lang document maar ook een simpel document";
var tekstvr = tekst.replace(woord,nieuwewoord);
</script>
I hope someone could help me! And i hope this isn't also another dupe (tried being more specific this time) Thanks anyway!
Upvotes: 0
Views: 80
Reputation: 3344
const tekstDiv = document.getElementById('tekst')
const tekst ="Dit document is een lang document maar ook een simpel document";
tekstDiv.innerHTML = tekst;
const btn = document.getElementById('btn');
btn.addEventListener('click',(event)=>{
const woord = document.getElementById('woord').value;
const nieuwewoord = document.getElementById('nieuwewoord').value;
tekstDiv.innerHTML = tekst.replace(woord,nieuwewoord);
});
<input id="woord" type="text" name="woord" placeholder="Wat moet er weg?">
<input id="nieuwewoord" type="text" name="nieuwewoord" placeholder="Wat moet er staan?">
<input id="btn" type="button" value="Vervang">
<div id="tekst"></div>
Upvotes: 2