Finding id text element



  • We need to put div inside div with constantly changing id. The only criterion can be found in div. Is it possible to receive an id element on the text inside it? Example:

    <div id="123-dx-fe45">Привет</div>
    

    We need to find out id div where there's a word. Привет(using javascript only).



  • ~function () {
      var divs = document.querySelectorAll("div[id]");
    

    for (var q=0; q<divs.length; ++q) {
    if (!divs[q].firstElementChild && divs[q].textContent.match(/Привет/i)) {
    divs[q].className = 'found';
    }
    }
    }();

    .found:after {
    content: " - " attr(id);
    color: red;
    }
    <div id="any">
    <div id="1">Привет</div>
    <div id="2">Пока</div>
    <div id="3">И тебе привет</div>
    <div>Привет</div>
    </div>
    <div id="4">Привет</div>
    <div id="5"><span>Привет</span></div>
    <div id="6"> Привет </div>
    <div id="7"> Всем привет! </div>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2