JavaScript Select all element with same tag name using javascript and highlight content partially select element with attribute,select element with class,select element

I made a code that should highlight searched string but it is not working.
Here is the code:

<body>

  <div>div is here</div>
   <div id="divid">

    <div>this is a div 1</div>
    <div> this is a div 2</div>
    <div> this is a div3</div>

  </div>

  <div> another div is here</div>
  </body>  

Here is a javascript code.

  function checkit(){

var hlWord = "div";
        var nregex = new RegExp(hlWord,"gi");
        var div = document.getElementById("divid").getElementsByTagName('div');

        for(var i=0; i <= div.length; i++){

            var div1 = div[i].innerHTML;

            var rword = div1.replace(nregex,"<b>"+hlWord+"</b>");
            div1.innerHTML = rword;

        }


  }  
Answer:1

There are begginer mistakes in your code. Let me correct them:

  function checkit(){

    var hlWord = "div";                  //Define string that will be emphasized by <b> tag
    var nregex = new RegExp(hlWord,"gi");//Create global, case-insensitive regexp
    var div = document.getElementById("divid").getElementsByTagName('div');  //Get element collection of divs in div#divid

    for(var i=0; i < div.length; i++){  //Loop through my element collection

        var div1 = div[i].innerHTML;    //Get the innerHTML of on of the divs

        var rword = div1.replace(nregex,"<b>"+hlWord+"</b>");  //Surround my string with <b>
        div[i].innerHTML = rword;  //Change the innerHTML back

    }

  }  
  1. You used this for condition: i<=div.length. This is wrong. Do not forget, that we count from 0 so: [0, 1, 2, 3].length = 4. Last element for such array has index 3. The [] is an array literal.

  2. By mistake, you assigned div1.innerHTML. div1 was a string. The element you want to change is div[i].

I made a JSFiddle too!

Answer:2

The problem with you code will be, amongst other problems, that nested div elements will be broken. You should use some kind of recursion if you want to highlight the word 'div'.

Here is such a function:

function highLight(term,root){
    var allDiv = root.querySelectorAll('div'),
        replacer = function(a){return '<span class="highlight">'+a+'</span>'};
    for (var i=0; i<allDiv.length; i+=1){
        if (allDiv[i].querySelectorAll('div').length){
            highLight(term, allDiv[i]);
        } else {
          var re = RegExp('('+term+')','gi');
          allDiv[i].innerHTML = allDiv[i].innerHTML.replace(re,replacer);
        }
    }
}

And here is a jsfiddle to play around with it

A more advanced jsfiddle

Answer:3

I'm making some kind of pinterest backbone application. (post pics/vids/text etc to remember later) It is the first time I try to make a register/login in backbone. I have a homeView and an ...

I'm making some kind of pinterest backbone application. (post pics/vids/text etc to remember later) It is the first time I try to make a register/login in backbone. I have a homeView and an ...

I'm doing JavaScript in node.js. I'm trying wrap an object not yet constructed, allowing calls to any object function and set/get any object attribute through the wrapper, letting the wrapper pass ...

I'm doing JavaScript in node.js. I'm trying wrap an object not yet constructed, allowing calls to any object function and set/get any object attribute through the wrapper, letting the wrapper pass ...

You are programming a lib X that depends on lib Y. You want that library to work on node.js and on the browser. On node.js, you'd use require('Y'). On the browser, you could use require.js. What's the ...

You are programming a lib X that depends on lib Y. You want that library to work on node.js and on the browser. On node.js, you'd use require('Y'). On the browser, you could use require.js. What's the ...

  1. module system of study
  2. module system and services inc
  3. module system of education
  4. module system for c++
  5. module system for javascript
  6. module of system
  7. system module for project

I'd like to create a simple BMI calculator (MS Dynamics 2011) I have 3 fields: 1: persons height (precision 2 - decimal formatted text box) 2. persons weight (precision 2 - decimal formatted text ...

I'd like to create a simple BMI calculator (MS Dynamics 2011) I have 3 fields: 1: persons height (precision 2 - decimal formatted text box) 2. persons weight (precision 2 - decimal formatted text ...

  1. calculation syntax in tableau
  2. calculation syntax in sql