How to find a proper selector of the HTML elemtn using JQuery

Viewed 91

I have a button in html below that has class 'hapus'. When I executed

alert($(this).closest("li").html());

It produces alert like this:

JENIS AKTIVA: 
<button class="btn-link input-xs tambah" type="button" style="display: none;">
   <a>+ tambah</a>
</button>
<div class="tingkat form-group form-inline"><br><button type="button" class="hapus form-group form-inline btn btn-sm btn-danger input-xs">X</button><select class="form-control form-control-sm input-xs turunan" style="width: 160px; margin-left: 2px;" required="" disabled=""><option disabled="" selected="" value="4">Infrastruktur dan Alat Non Medis</option></select> Ket: <input type="text" class="form-control form-control-sm input-xs" style="margin-left: 7px;" name="125" value=""></div>

The question is how can I check if the html of "li" I targeted contains the word 'JENIS AKTIVA'?

2 Answers

Use:

$(this).closest("li").text().includes('JENIS AKTIVA')

Which will return true or false, which you can use in an if statement.

Try testing against a regular expression.

 element = $(this).closest("li").html();
 myRegEx = /\s+JENIS AKTIVA\s+/i;
 console.log(myRegEx.test(element))

That should return either true or false.

Related