0

I was just wondering if there would be away i can check if a last div with same class name contains a certain word and if the certain word exists run a function.

every time i tried to search for help all i find is all in jquery

var words =  #hey, #join, #slap
var nodes = document.querySelectorAll('.hud-chat-message');
var last = nodes[nodes.length- 1];

// Check for certain words here in nodes last
<div id="hud-chat" class="hud-chat">
  <div class="hud-chat-message">
    <strong>Name</strong>: text here
  </div>
  <div class="hud-chat-message">
    <strong>Name</strong>: text here
  </div>
  <div class="hud-chat-message">
    <strong>Name</strong>: just more text here
  </div>
  <div class="hud-chat-message">
    <strong>Name</strong>: text here
  </div>
  <div class="hud-chat-message">
    <strong>Name</strong>: /join
  </div>
  <div class="hud-chat-message">
    <strong>Name</strong>: text here
  </div>
  <div class="hud-chat-message">
    <strong>Name</strong>: /slap
  </div>
</div>
Girisha C
  • 1,922
  • 1
  • 12
  • 20
0B1EYES
  • 3
  • 2
  • 1
    I'm afraid it's not clear whether you're asking how to tell *whether* `last` contains a word, or how to find the last div with that class that does...? (And in your example, what word are you looking for?) – T.J. Crowder Oct 12 '18 at 11:34
  • i want to know if i can check if last contains certain word can this be done? – 0B1EYES Oct 12 '18 at 11:36
  • Of course you can. You get the text (from [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent), [`innerText`](https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText), or [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML)) and then look to see if the word is in that text. – T.J. Crowder Oct 12 '18 at 11:40
  • Question was closed before i could answer: https://jsbin.com/suciweminu/edit?html,js,console,output Here you got! – Tstar Oct 12 '18 at 11:41
  • [`const found = words.some(word => node.textContent.includes(word));`](https://jsfiddle.net/smu39kx1/1/) – Andy Oct 12 '18 at 11:44

1 Answers1

1

You can do it like this. The words should be an array containing all the words. Then you simply iterate over the words and check if they are part of the string using indexOf.

var words =  ['hey', 'join', 'slap'];
var nodes = document.querySelectorAll('.hud-chat-message');
var last = nodes[nodes.length- 1];

for(var i = 0; i < words.length; i++) {
  if(last.innerText.indexOf(words[i]) > -1) {
    console.log('Found: ' + words[i]);
  }
}
<div id="hud-chat" class="hud-chat">
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: just more text here</div>
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: /join</div>
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: /slap</div>
</div>
Cata John
  • 1,371
  • 11
  • 19