Note that there are some explanatory texts on larger screens.

plurals
  1. POFind text in div and goto text using javascript
    primarykey
    data
    text
    <p>Hi I'm trying to achieve the following: I'm creating a site (actually some sort of web app) that consists of panels (divs).</p> <p>On one of the panels I have a substantial amount of text placed in another div:</p> <pre><code> &lt;div class="panel"&gt; &lt;div id="substantialText"&gt; bla bla bla bla &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I need to be able to search this text using an input field in a header and goto text location like so (typing text in input field will call the function "findText()"...I need to find out what to write in this function:</p> <pre><code> &lt;div class="panel"&gt; &lt;header&gt; &lt;div&gt; &lt;input type="text" id="searchField" onKeyUp="findText();" /&gt;&lt;br/&gt; &lt;/div&gt; &lt;/header&gt; &lt;div id="substantialText"&gt; bla bla bla bla &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I have got the following but am stuck (comparing the innerhtml of the text to the value of the searchfield):</p> <pre><code> function findText() { var streng = document.getElementById(substantialText).innerHTML; var search = document.getElementById(searchField).value.toString().toLowerCase(); if(document.getElementById(felt).value.length == 0) { //don't judge me } else { var rgxp = new RegExp(search, "gi"); // Getting number of hits...works as intented alert(streng.match(rgxp).length); } } </code></pre> <p>I'm thinking about using the array of hits somehow paired with their location, and then being able to cycle through the hits using the index of the array. But I cannot see how I would achieve this.</p> <p>How can I get the 'substantialText' to scroll down to the hits (given an index). I have successfully been able to jump to an element containing ID or anchor but I cannot wrap my head around how to achieve the same with just a textstring.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload