Templates
Plugins
Record
Shapes
Emojis
SFX
Help
You can record up to 15 minutes each time. Click the button below to start recording your screen. You will be prompted to share your screen with Kapwing.
Audio
Layers
Templates
Plugins
Record
Shapes
Emojis
SFX
    Kapwing Logo
    Untitled Project
    Upload
    Add Subtitles
    Press to upload
    or, drag and drop a file here
    Create with Kapwing AI
    Upgrade
    Settings
    Export
    Sign In
    Help
      Kapwing Logo
      Untitled Project
      Upload
      Add Subtitles
      Create with Kapwing AI
      Upgrade
      Start with a blank canvas
      21:9
      LinkedIn Landscape
      16:9
      Youtube
      1:1
      Profile Photo / Meme
      4:5
      Instagram Post
      Record Screen and Camera (Beta)
      Start Recording 15min limit
      Include Microphone
      record icon
      Include Camera
      Untitled Project
      Upload
      Add Subtitles
      Create with Kapwing AI
      Upgrade
      Comments
      Settings
      Export
      Sign In
      JavaScript - Search for text in HTML page
      0102030405060-language-keyboard.xv2.us

      JavaScript - Search for text in HTML page

      //Contact

      Preface

      Probably I should start this blog post with a big disclaimer, because so far doing anything with JavaScript to me was like entering a world of pain (sorry, but the movie reference is a must). Furthermore I see a really huge risk here that some of my collegues – those who really know how to code JavaScript – will be tearing this blog post apart. But anyway I will share my experiences in solving the above mentioned problem which is: “Search for some text in a HTML page and then jump to the first occurence of that text found.”

      Maybe the worst thing of all is that all this is happening during a fun project of mine and therefore – so to say – doing some JavaScript here was my free will. Well, my usual solution to any JavaScript-related question is: Google or my colleague Fabian. But somehow this time I did not really find a satisfying answer with Google and did not want to bother anyone with this. Thus I ended up reading the W3School’s JavaScript Tutorial. (Edited 16.11.2013: Please see comment in the comments section below on W3School.)

      The Problem in Detail

      The problem to solve can be best shown by example. I am pretty sure it should be relatively easy to apply the solution to this on similar problems then. The webpage to be searched lists technical phrases in english language, gives a translation into german and adds a short explanation to each term. Using Bootstrap for this it happended that the web page contains entries like the following (quite a lot of them):

      1<div class="row" id="graphic_board">
      2   <div class="col-md-2">
      3      <p><b>graphic board</b></p>
      4   </div>
      5   <div class="col-md-3">
      6      <p><i>Grafikkarte</i></p>
      7   </div>
      8   <div class="col-md-7">
      9      <p>Some short description here ...</p>
      10   </div>
      11</div>

      So the task is to search through a whole lot of such entries and then jump to that section which is defined by the id-field of the first div-element when the search-string has been found.

      Of course some kind of search-form is required, which could look like this for example:

      1<form id="searchForm" action="javascript:search();">
      2   <div class="input-group">
      3      <button id="go" class="btn btn-default" type="button" 
      4              onclick="document.getElementById('searchForm').submit(); return false;">
      5      </button>
      6      <input type="text" id="searchItem" class="form-control" placeholder="Suchbegriff">
      7   </div>
      8</form>

      It is a rather simple HTML-form that can be used to enter the search-string and to then submitting the form. On submitting the form the action to be performed is defined as executing the JavaScript-method “search”.

      That method is explained in the following.

      The Solution

      The solution is based on a rather simple idea. Just get all elements on the page that have a certain class-name (CSS-class). In this case when searching for the english text that class-name is “col-md-2”. Of course it might make sense to add a kind of empty class here that is only used for the sole purpose of searching for it. This way we are getting an array of elements which can then be looped and traversed to find sub-elements like the “p”-element that is containing the actual text.

      Some simple string-method can be used to perform the actual search for the search-string. Once that string is found we are searching for the id-attribute of the parent div-element. Then we can jump onto that very position in the page.

      The resulting JavaScript then looks like this which is hopefully pretty straightforward.

      1<script>
      2function search() {
      3 
      4   var name = document.getElementById("searchForm").elements["searchItem"].value;
      5   var pattern = name.toLowerCase();
      6   var targetId = "";
      7 
      8   var divs = document.getElementsByClassName("col-md-2");
      9   for (var i = 0; i < divs.length; i++) {
      10      var para = divs[i].getElementsByTagName("p");
      11      var index = para[0].innerText.toLowerCase().indexOf(pattern);
      12      if (index != -1) {
      13         targetId = divs[i].parentNode.id;
      14         document.getElementById(targetId).scrollIntoView();
      15         break;
      16      }
      17   }  
      18}
      19</script>

      One nice thing about this is that it could be easily enhanced to search in the other div-elements as well and by ordering this search one could weight what is more important. For example first finding text that defines a term and only then search the descriptions.

      What should I say: Somehow I have started to like this. Of course I know that doing JavaScript without any additional framework is considered (very) old school and thus I probaly have to read this next 🙂 .

      Start with a blank canvas
      21:9
      LinkedIn Landscape
      16:9
      Youtube
      1:1
      Profile Photo / Meme
      4:5
      Instagram Post
      9:16
      TikTok / Instagram Story
      or
      Comments
      Upload
       
      Media button
      Media
      Subtitles
      Text
      Start with a blank canvas
      21:9
      LinkedIn Landscape
      16:9
      Youtube
      1:1
      Profile Photo / Meme
      4:5
      Instagram Post
      9:16
      TikTok / Instagram Story
      or
       
      Videos
      Images
      Elements
      Untitled Project
      Upload
      Add Subtitles
      Create with Kapwing AI
      Upgrade
      Comments
      Settings
      Export
      Sign In
      Start with a blank canvas
      21:9
      LinkedIn Landscape
      16:9
      Youtube
       
      Media button
      Media
      Subtitles
      Text
      Videos
      Images
      Elements
      Audio
      Layers
      Templates
      Plugins
      Record
      Shapes
      Emojis
      SFX
      Help
      9:16
      TikTok / Instagram Story
      or
      Shapes
      Emojis
      SFX
      Help
      Media button
      Media
      Subtitles
      Text
      Videos
      Images
      Elements
      Audio
      Comments
      Settings
      Export
      Sign In
      Layers
      Templates
      Plugins
      Record
      1:1
      Profile Photo / Meme
      4:5
      Instagram Post
      9:16
      TikTok / Instagram Story
      or
       
      Record
      Record Screen and Camera
      Record your screen and computer camera at the same time, with or without microphone.
      Record Screen
      Record with or without camera, microphone, and screen cast.
      Record Camera
      Record video and audio from your computer camera and microphone.
      Record Audio
      Record audio from your computer microphone.
      Media button
      Media
      Subtitles
      Text
      Videos
      Images
      Elements
      Audio
      record icon
      Make sure everything is in frame! Camera preview will not be available once recording starts. Turn on your camera to see the camera preview.
      Ads keep us online. Without them, we wouldn't exist. We don't have paywalls or sell mods - we never will. But every month we have large bills and running ads is our only way to cover them. Please consider unblocking us. Thank you from GameBanana <3
    1. neon-line-streets.xv2.us
    2. neon-line-streets.xv2.us
    3. ONLINE RANDOM COLOR GENERATOR | Generate random colors, you can fix some values or generate a completely random color

      XV.us.color Selector

      HexC9BD23
      RGB201 ,189 , 35
      HSV0.155 ,0.826 , 0.788
      HSL0.155 ,0.703 , 0.463
      Piano Tutorial - OnlinePianist
    4. indrevus
    5. 01020-neon-line-streets.xv2.us
    6. 01020-neon-line-streets.xv2.us
    7. 0102-neon-line-streets.xv2.us