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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With