JQuery Get the last of several class values
I have a couple of divs that look like this:
<div class="film cars-0317219"></div>
<div class="film wall-e-0910970"></div>
<div class="film finding-nemo-0266543"></div>
<div class="film cars-0317219"></div>
I am interested in the second (so last) class name, but only those divs that have the movie class. Is there a way to get the last class name using jQuery?
To give you an example of what I want to see below. I want to make these divs accessible and allow the visitor to go to / title / last -class-name.html
$('div.film').live('click', function(){
// This returns the whole 'film random-title-id'
// instead of just 'random-title-id'
var id = $(this).attr('class');
location.href = '/title/' + id + '.html';
});
+5
1 answer
This should work:
$('div.film').live('click', function(){
var classes=$(this).attr("class").split(" ");
var id=classes[classes.length-1];
location.href = '/title/' + id + '.html';
});
although if you don't have identifiers for your elements yet, it looks like this will make more sense as an identifier.
+7