JavaScript – Search for text in HTML page



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):

<div class="row" id="graphic_board">
   <div class="col-md-2">
      <p><b>graphic board</b></p>
   <div class="col-md-3">
   <div class="col-md-7">
      <p>Some short description here ...</p>

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:

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

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.

function search() {
   var name = document.getElementById("searchForm").elements["searchItem"].value;
   var pattern = name.toLowerCase();
   var targetId = "";
   var divs = document.getElementsByClassName("col-md-2");
   for (var i = 0; i < divs.length; i++) {
      var para = divs[i].getElementsByTagName("p");
      var index = para[0].innerText.toLowerCase().indexOf(pattern);
      if (index != -1) {
         targetId = divs[i];

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 🙂.

Long-term experience in agile software projects using
Java enterprise technologies. Interested in test automation tools and concepts.


  • Ben Ripkens

    Hi Thomas,

    nice to see that you are getting into JavaScript and that you are sharing your experiences :-).

    Not related to your article’s actual content, but needs to be mentioned: You might not want to use W3Schools as some kind of reference documentation or even for tutorials. W3Schools is bad and there is even a dedicated website by some of the most important people in the field that tells you why you shouldn’t use it [1]. A proper alternative would be the Mozilla Developer Network [2] or [3].

    Btw.: Doing JavaScript without frameworks is great :-)!


  • Thomas Jaspers

    Hi Ben,

    thanks a lot for the comment and the hint on W3Schools. Adjusted the blog post according to this. I will for sure check out the pages you mentioned :-).

    – Thomas


Your email address will not be published.