Cesar Cesar - 1 year ago 50
jQuery Question

Access and change html of divs with a given string ID in jQuery

I am using the following code to access all the div which have a given string ID (those starting with the string "brand":

var tipo = "brand".
jQuery('div[id ^= "' + tipo + '"]').hide();

It works perfectly, and hide all the divs at once. But I would like to get a list (array) of all id's affected to change its .html(), but don't know how to achieve this. I have tried.

jQuery('div[id ^= "' + tipo + '"]').each(

to show an alert for each ID found, but no success. How should I proceed?

Answer Source

You should pass anonymous function to each method:

jQuery('div[id ^= "' + tipo + '"]').each(function() {