views:

186

answers:

4

I have the following:

<div id="unique" class="myclass xyz_345">...</div>

Question: How do I access what # is after xyz_, given that I have 2 css classes being applied to it? (In the example above, it would be the number 345).

I've tried the following, but it does work:

var num_after_xyz = $('unique').attr("class").substring(4);
+1  A: 

I'm pretty sure that

var myString = $("#unique").attr("class")

is going to return "abc xyz_345". So then you could split that string on space:

var mySplitResult = myString.split(" ");

and iterate through the resulting array looking for something that starts with "xyz_" - and then taking the substring of that element.

var s;
for (s in mySplitResult){
     if(s.substring(0,4) == "xyz_"){
          var numberAfterXYZ = s.substring(4);
     }
}
Austin Fitzpatrick
So I tried, var num_after_xyz = $('unique').attr("class").split(" ").substring(4); and that doesn't work. What am I doing wrong?
TeddyG
numAfterXyz = $('unique').attr("class").split(" ").substring(4);
TeddyG
Wait no. Split is going to return an array. You'll need to look through each element in that array for "xyz_" before taking the substring of that particular element.
Austin Fitzpatrick
You should see what `$('unique').attr("class")` returns, and work from there
Isaac Cambron
`split` returns an array and not an object and `for (… in …)` is suitable for arrays. Use `for (…;…;…)` instead.
Gumbo
+1  A: 
var num_after_xyz = $('#unique').attr("class").match(/^.*_(\d+).*/);
alert(num_after_xyz[1]);

This regular expression should work.

It looks for anything, followed by _ and some digits, followed by anything.

patrick dw
I used: $('#unique').attr("class").match(/^.*_(\d+).*/)[1]; and that works.
TeddyG
nice, a lot more concise than mine ;p you could wrap this in a simple plugin to allow searching for the number after a specific prefix (in case you have multiple possible class prefixes)
Rowan
+1  A: 

Try something along these lines:

jQuery.fn.getClassNumber = function(classPrefix){
    var classes = this.attr('class').split(" ");
    jQuery.each(classes, function(i){
        if (classes[i].substring(0,classPrefix.length-1)==classPrefix){
            return classes[i].substring(classPrefix.length-1);
            }
        });
    return false;
    }

Then to get the number after the class prefix:

$('element').getClassNumber('xyz_');

I think this will work but I haven't tested it, give it a go!

Rowan
A: 

Let's make a little plugin for this:

/**
 * ReplaceClass
 *
 * @param   regexp|substr       The old classname you are looking for
 * @param   newSubStr|function  The new classname
 * @see     https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
 * @depends jQuery
 * @author  @abernier
 */
(function($) {
    $.fn.replaceClass = function(oldClass, newClass){
        return this.each(function(i, el){
            $(el).attr('class', function(index, attr){
                return attr.replace(oldClass, newClass);
            });
        });
    };
})(jQuery);

eg:

$('<div class="foo bar"></div>').replaceClass('foo', 'bar');
# => [div.bar.bar]

or with regexp:

$('<div class="foo-25 bar"></div>').replaceClass(/foo-[0-9]{2}/, 'foo-100');
# => [div.foo-100.bar]
abernier