views:

251

answers:

4

Hello, I'm looking for a way using jquery/javascript to get a class name when I only know part of it and there are more than one classes applied to that element.

So if I'm looking for a class that I know starts with "charLimit_" I want to know that full class name even when it's not the only class applied to a given element. Example HTML would be.

<input class="charLimit_30 lastNameValidation" type="text" />

What I have so far below will put both "charLimit_30" and "lastNameValidation" into an array, but how do I then say I only want "charLimit_30" and then only tell me the value found after the underscore (30).

var classMName = $('[class*=charLimit_]').attr("class").split(' ');

Thanks in advance for you help!!!

A: 
var classes = $('input').attr('class');
var myNum = '';
for(var i=0;i<classes.length;i++){
    if(classes[i].match(/^charLimit_.*$/){
        myNum = classes[i].split('_')[1];
    }
}

Something like that would work, unless of course there's some jQuery magic way of doing it that I'm simply unaware of :)

inkedmn
This would end up with myNum = "30 lastNameValidation"... I tried out your script and you're missing a ")" after the match. Also the classes variable ends up as a string and not an array.
fudgey
Would that do it: classes[i].match(/^charLimit_(\\d+)(\\s+)/) ? (Not so good at regex)
SeanJA
+2  A: 

If you only need first occurrence and everything that follows underscore in class is a number, then something like this would do it:

$('[class*=charLimit_]').attr("class").match(/(?:\s|^)charLimit_(\d+)/)[1];
kangax
Correct and simple, but the jQuery style guide eschews "match" in favor of "test" and "exec." See http://docs.jquery.com/JQuery_Core_Style_Guidelines
James A. Rosen
@Gaius Interesting. I suppose `exec` could be slightly faster in this case.
kangax
This answer works perfectly for me if you remove the [0] after the attr("class").
fudgey
Ah yes, I forgot that `attr` returns a string, not an array :) Thanks, I changed answer appropriately.
kangax
+1  A: 

This is kinda like what inkedmn said, but in a more jQuery-ish way:

var numbersAfterCharLimit = [];

$('[class*="charLimit_"]').each(function() {
    var matches;
    if (matches = this.className.match(/(?:\s+|^)charLimit_([0-9]+)(?:\s+|$)/)) {
        numbersAfterCharLimit.push(parseInt(matches[1], 10));
    }
});
nickf
Nope, can't use /\b/ there : )
kangax
why not?
nickf
And if you're already using `parseInt`, I would advice to supply `radix` (i.e. `10`). You don't want to end up translating '08' into '0'.
kangax
@nickf, because class values are delimited with whitespaces, and your boundary will match "foo" in "foo-bar baz", although it shouldn't. It's a pretty common mistake. I've seen dozens of `hasClassName` implementations where people fall into this trap and use `"\b" + className + "\b"` instead of a proper - `(?:\\s+|^)` + className + (?:\\s+|$)`.
kangax
i realised the mistake (that it'd match `-`) just after I commented, but I'm glad I waited for your response! Thanks for the great advice!
nickf
hey guys, thanks for the help. I'm working with the latest revised code here and I cannot get it to enter the "if" statement if the class looks something like this class="dogs charLimit_20 cats" or in other words if there are 1 or more classes surrounding the "charLimit" class. Any idea what I'm doing wrong?
72Vine
Hey Vine, i've fixed two bugs in that code again (you can't do `if (var matches = ...)` and i had double-slashed the `\s` when I didn't need to. That should work now.
nickf
Nick, thanks so much for the help! This works great for me.
72Vine
+1  A: 

This appears to work (at least on stackoverflow):

var test = $('[class*=owner]').attr('class').split(' ');
for(i in test){
    if(test[i].indexOf('post-sign') !== false){
        test = test[i].split('-');
        // replace this with a return
        console.log(test[1]);
        break;
    }
}

And one that will work with the code that he is trying to do it with:

var test = $('[class*=charLimit_]').attr('class').split(' ');
for(i in test){
    if(test[i].indexOf('charLimit_') !== false){
        test = test[i].split('_');
        // replace this with a return if it is in a function
        console.log(test[1]);
        break;
    }
}
SeanJA
Sometimes I think I overuse string functions...
SeanJA
how does this answer the question?
nickf
It returns the word 'sign'... equivalent to the '30' in his question.
SeanJA
"tell me the value found after the underscore (30)."
SeanJA
@nickf: better?
SeanJA