TransWikia.com

How to find out if A contains B? In javascript

Stack Overflow Asked by Jkil on January 30, 2021

Let’s say I have two divs A and B.

<div id="A">1253555</div>

<div id="B">1</div>

I have to find out if A contains B.
In this example, it will be the answer of (if 1253555 contains 1 ), which should be true.
Then i have to execute function doSomething().

function doSomething() {

  var x  =  document.getelementById("A").innerHTML;

  var y  =  document.getelementById("B").innerHTML;

  if (=...) { 
 // I dont know how to code here now...i tend to use indexof

}

Please guide me further.
Please use a method in pure javascript and works best for all browsers.

6 Answers

The code concept is simple just get the div innerHTML value and use includes in the javascript you will get the result you wan : D

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <div id="a">1234</div>
    <div id="b">1</div>
</body>

<script>
    var a1 =  document.getElementById("a").innerHTML;
    var b1 =  document.getElementById("b").innerHTML;

    if(a1.includes(b1)) alert("true"); // you can change the alert to do whatever function you wan :D
    else alert("false");
</script>
</html>

Answered by avoidToxicMuted on January 30, 2021

You can use the "includes" method.

function doSomething() {
   var x  =  document.getElementById("A").innerHTML;
   var y  =  document.getElementById("B").innerHTML;

   if (x.indexOf(y) !== -1) {
    console.log("Perform your next task!!!")
   }
}

doSomething();

Answered by yuvrajgawade on January 30, 2021

The correct syntax is document.getElementById() (capitalize 'E' of Element), then you can use includes() string method in your conditional. Take care about security issues of innerHTML, you can use instead textContent, you can read about this: https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML (security considerations section)

  var x  =  document.getElementById("A").textContent;
  var y  =  document.getElementById("B").textContent;

  if (x.includes(y)) { 
    console.log('yes')
    }
}

Answered by sonEtLumiere on January 30, 2021

more complete code...

const bTest = document.getElementById('bt-test')
  ,  divA   = document.getElementById('div-A')
  ,  divB   = document.getElementById('div-B')
  ;
bTest.onclick=()=>
  {
  if ( divA.textContent.includes( divB.textContent ))
    alert( 'yes it include')
  else
    alert( 'nope!')
  }
div {
  border: 1px solid grey;
  padding: .5em;
  margin: .5em;
}
<div id="div-A" contenteditable>1253555</div>

<div id="div-B" contenteditable>1</div>

<button id="bt-test"> test include </button>

Answered by Mister Jojo on January 30, 2021

Just use includes:

if(x.includes(y)){
  console.log('true');
}

Answered by Arpan Kc on January 30, 2021

You can just use the String.prototype.includes(other) function.
Documentation

function doSomething() {

  var x  =  document.getElementById("A").innerHTML;

  var y  =  document.getElementById("B").innerHTML;

  if (x.includes(y)) console.log("It does");
  else console.log("It doesn't");

}

doSomething();
<div id="A">1253555</div>

<div id="B">1</div>

Answered by Samathingamajig on January 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP