Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if array value contains specific value in html

Tags:

html

angularjs

I have a array in angularjs like below:

var test=["abc/1/1","def/2/2","efg/3/3"];

I want to check if an array contains value having "abc" in it in html

I checked:

test.indexOf("abc")!=-1

but its returning false

I have populated array in JS and i want to check if array contains value containing abc in html.

button form="myAttributeAdd" type="submit" data-ng-disabled="" class="btn btn-success btn-xs"> Save

This is button element in my html. here data-ng-disabled will be true or false depending on array data.If array data value contains abc it will be enabled else disabled

like image 373
Sayali Dingankar Avatar asked Jul 15 '26 13:07

Sayali Dingankar


1 Answers

You can use RegExp to search abc inside it.

//getting html value
var valueToFind = document.getElementById('htmlCode').innerText;

function findValue(findString) {
  //getting arrays to be find
  var test = ["abc/1/1", "def/2/2", "efg/3/3"];
  //creating the regex
  var reg = new RegExp(findString);
  
  //a.match will show the output
  console.log(findString,test.some(a => a.match(reg)))

  console.log(findString,test.filter(a => a.match(reg)))

}

//invoking the function
findValue('abc');
findValue(valueToFind);
<div id="htmlCode">efg</div>
like image 120
Just code Avatar answered Jul 17 '26 20:07

Just code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!