i am breaking sentences into <span class="word"> and then <span class='letter'> through this simple js and its working demo:
<h1 id="text">
this is demo text that is breaking word into span letter
</h1>
<style>
.word {border: 1px solid red;}
.letter {border: 1px solid lightblue;}
</style>
<script>
var textWrapper = document.querySelector('#text');
textWrapper.innerHTML = textWrapper.textContent.replace(/(\S*)/g, m => {
return `<span class="word">` +
m.replace(/(-|)?\S(-|@)?/g, "<span class='letter'>$&</span>") +
`</span>`;
});
</script>
But now i want that sentences should be break into <span class="word"> and then <span class='letter'> only if when Checkbox is active else this is not should be break.
i tried these code but it's not working, what mistake am making can i know plz stackoverflow community.
<label for="myCheck">Checkbox:</label>
<input type="checkbox" id="myCheck" onclick="myFunction()">
<h1 id="text" >Checkbox is CHECKED!</h1>
<script>
function myFunction() {
var checkBox = document.getElementById("myCheck");
var textWrapper = document.querySelector('#text');
if (checkBox.checked == true){
textWrapper.innerHTML = textWrapper.textContent.replace(/(\S*)/g, m => {
return `<span class="word">` +
m.replace(/(-|)?\S(-|@)?/g, "<span class='letter'>$&</span>") +
`</span>`;
});
} else {
textWrapper.style.display = "block";
}
}
</script>