Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery. Select all the elements that classname starts with

If I have elements with classnames like this:

   .ses_0 .ses_1 .ses_2 .ses_3  

How can I select all the elements and prepend those with some snippet? Something like this:

    var sessions = $('*[class*=ses_]');

    for (var i = 0; i < sessions.length; i++)
    {
        sessions [i].prepend("<img src='/Content/img/caticon"+i+".png' />");
    }

That doesn't work of course.

Edit: Ahhhh... damn It seems that I need to get not only those classes that start with .ses_ but also <a> elements within. How can I do that?

Basically something that works $(".ses_0 a") , only I need to get all the classes start with ses_

like image 754
iLemming Avatar asked Mar 23 '11 21:03

iLemming


1 Answers

You're almost there:

// selects all that start with "ses_"
var sessions = $('[class^="ses_"]');

Even though your loop should work, you could also use the

sessions.each(function(index){
    this.prepend(... // and so on
});
like image 93
typeof Avatar answered Nov 05 '22 19:11

typeof