2

I have a span with a certain class and I want to check if it contains it and hide another element.

To be more specific I have:

<span class="border">0€</span>
<div class="pop">something</div>

I tried the code but unfortunately it does not work.

if ($(".border").html().indexOf("span") == 0€ ){
    $("div[name=pop]").hide()
}

Looking forward hearing from you.

messivanio
  • 2,263
  • 18
  • 24

3 Answers3

3

The indexOf function returns the index and you can not compare it with string you are looking for 0€.

if ($(".border").html().indexOf("0€") != -1 ){

You probably need text instead of html as this string is not expected as part of html.

if ($(".border").text().indexOf("0€") != -1 ){

Edit based on comments of OP that he added as an answer.

TypeError: $ is not a function

You need to add the reference of jQuery in order to use jQuery function like $ for which you are getting error. This question Is there a link to the “latest” jQuery library on Google APIs will show you have you can add jQuery.

Is there a link to the "latest" jQuery library on Google APIs?

Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Use :contains pseudo-selector:

Select all elements that contain the specified text.

if ( $('.border:contains("0€")')

UPDATE

If you want to check if it contains specifically 0€

if ( $('.border:contains(" 0€")') // This will not select 100€

Assuming there is space before 0€

Tushar
  • 85,780
  • 21
  • 159
  • 179
0

Don't use :contains() or indexOf() as it will fail in cases like 100€

if ($("span.border").html().trim() == '0€') {
    $("div[name=pop]").hide()
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531