Identify DIV by complex id

Asked

Viewed 81 times

1

I have several Divs dynamically loaded via PHP like this:

<div class="projeto" id="-9-3-21"></div>
<div class="projeto" id="-3-1-77"></div>
<div class="projeto" id="-5-33-1"></div>
<div class="projeto" id="-9-23-11"></div>
<div class="projeto" id="-1"></div>

A JS file receives an action when you click on some buttons, and the answer is a number. What I need is:

When you click the number 33 (example), the jquery fades on the Ivs that have this number, in case it would be this id="-5-33-1"

It is possible?

  • Seria 33 in the field id ? or in the text of div ?

2 answers

3


Yes, use the * selector in Jquery, which will select everything that contains such a value. I put an example on Jsfiddle for you to see

In my case, I created a span and hid the div that has id with its text.

<span id="texto">33</span>

//No clique do span, esconda aqueles com o id que você quer
$('#texto').on('click',function(){
    $('div[id*="' + $(this).text() + '"][class="projeto"]').fadeOut()
});
  • Hi, it worked. But if I have another div in another area of the site with an id with the number 33 (example id="bt33"), it goes too. Can’t condition this only the div that have such class?

  • Sure, you can, I’ll fix it for you here and in the fiddle

1

You can use the Seletor Contais jquery.

$( "div[id*='@SEUVALOR']" ).fadeOut();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.