Catch all Divs with jquery

Asked

Viewed 421 times

1

I have several Ivs on my site, where they contain the following class "code$Numb" where $Numb is a random number, as do to capture all Ivs with the code regardless of $Numb?

  • 1

    It would be more efficient to add another fixed class to group them all, for example: code code$numb. This is very common in cloud tags.

  • That, but how would you capture them all?

  • Thus: $('.code')

1 answer

5


Like the utluiz referred in this case it may be more practical to create a class to group these elements and save this $numb otherwise (data- field for example).

But to answer your question about how to capture all elements with code, regardless of the $numb, you can use it like this:

$('div[class^="code"]');

where ^= means starting at...

Can read more about this selector here, but what it says in the description is:

Selects Elements that have the specified attribute with a value Beginning Exactly with a Given string.

Choose elements that have a specific attribute starting with exactly the specified string

Example: http://jsfiddle.net/4P3Ut/

  • I’m a layman in jquery, it would be so to capture all Divs?

  • @Viniciuseduardo, I put an example. This selector finds all that have class started by "code"

  • I like the code, simple.. But how to perform functions with it?

  • @Viniciuseduardo, as in the jsFiddle I put, an example where it replaces only the chosen elements and changes their content $('div[class^="code"]').text('encontrou!');

  • Yes, but I want to send a request with Ajax to each element..

  • @Viniciuseduardo, ok. And what do you want to do? load this element with content from ajax?

  • Yes! send a request ajax and put it in place..

  • Hmmmm... I see now that you’ve cleared the answer as accepted. I think I answered your question. You didn’t ask about Ajax in the question and I don’t understand why you canceled it... :/ Regarding AJAX take a look at the fiddle: http://jsfiddle.net/3QUr9/ If you don’t notice, ask a new question.

  • Hey, sorry.. By that point I’m right, but I didn’t want to spend time with one more question.. Thank you very much anything I come back :3

  • @Sergio is the guy! This good tips in javascript/jquery!

  • 1

    Just out of curiosity, in case the need were to end with "code" would be: $('div[class$="code"]')

Show 6 more comments

Browser other questions tagged

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