3

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

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

10 Comments

I have populated array in JS and i want to check if array contains value containing abc in html
Yes so you can replace your html value to RegExp('abc') and it should work. let me know if you find any difficulties.
can you please explain a bit more here
@SayaliDingankar Check my edit and comments you will understand
cant i do it in html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.