Osama ghanem Posted July 14, 2022 Share Posted July 14, 2022 Is it possible to edit the below code in order to read these 4 sentences (1) Lorem ipsum dolor sit amet. (2) consectetur adipisicing elit. (3) Similique aliquam totam odit excepturi. (4) reiciendis quam doloremque ab eius quos. from WebViewString Block blocks (2)1466×232 34.5 KB instead of reading them from the below html code let paragraph = document.getElementById('test'); paragraph.innerHTML = '<span>' + paragraph.textContent.trim().replaceAll(/\.\s/g,'. </span><span>') + '</span>'; let sentences = paragraph.querySelectorAll('span'); sentences.forEach(s => s.addEventListener('click', highlight)); function highlight(event) { sentences.forEach(s => s.classList.remove('selected')); event.target.classList.add('selected'); } span.selected { background: yellow; } <p id="test"> (1) Lorem ipsum dolor sit amet. (2) consectetur adipisicing elit. (3) Similique aliquam totam odit excepturi. (4) reiciendis quam doloremque ab eius quos. </p> Link to comment Share on other sites More sharing options...
dsonesuk Posted July 14, 2022 Share Posted July 14, 2022 (edited) Have tried split()? You can use '.' As a delimiter to store each sentence in an array and refer to index ref to select and manipulate. <!DOCTYPE html> <html> <body> <h1>JavaScript Strings</h1> <h2>The split() Method</h2> <p>split() splits a string into an array of substrings, and returns the array:</p> <p id="test"> (1) Lorem ipsum dolor sit amet. (2) consectetur adipisicing elit. (3) Similique aliquam totam odit excepturi. (4) reiciendis quam doloremque ab eius quos. </p> <p id="demo"></p> <script> let text = document.getElementById("test").innerHTML; const myArray = text.split("."); document.getElementById("demo").innerHTML = myArray[1]; </script> </body> </html> Edited July 14, 2022 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now