Reputation: 925
I'm trying to use jQuery to pass a height to colorbox. Doing so, the only way I can think to define the height is through the links 'class'. So I'm trying to figure out how to get the number from a string; e.g;
class="height_900" ; I want it to get the number, '900'.
Is this possible with jQuery?
MORE INFO: Ok.. So here's what I'm trying to achieve.
I'm creating a Wordpress Shortcode for colorbox (variation of lightbox). Colorbox sets its height and width via jQuery.
The Wordpress shortcode contains the attributes 'height' & 'width' that allow the user to set both manually if they wish. The problem is, I cannot figure out a way to pass these PHP variables to the colorbox jQuery.
The best solution I could come up with was to have a class that goes as follows: height_$height; e.g. height_450. I then wanted to get just the number via jQuery, then it uses that as that specific colorbox's height. Same for the width.
Upvotes: 2
Views: 9212
Reputation: 343
Here is a good way using parseInt()
if(Stringname.substr(-3)==parseInt(Stringname.substr(-3)))
var b=Stringname.substr(-3);
else if(Stringname.substr(-2)==parseInt(Stringname.substr(-2)))
var b=Stringname.substr(-2);
else
var b=Stringname.substr(-1);
It checks and give the correct answer and store it in variable b for 1 digit number and upto 3 digit number. You can make it to any if you got the logic
Upvotes: 1
Reputation: 322612
Late answer, but if you know the number of characters you'd need, I'd use .slice()
instead:
"height_900".slice(-3);
This will give you the last 3 characters in the string.
If you want the result to be a number, you can use the unary +
operator to convert.
+("height_900".slice(-3));
Upvotes: 0
Reputation: 19319
First of all in answer to your question. Make jQuery work for you. It has a .height()
method which will not only return the current height of the box but allow you to set it as well (e.g. $("#someID").height( 200 )
)
Secondly: CSS should be used to describe a thing, not the attribute of a thing. What if you want to come back later and change the height of your elements to 700 instead of 900? You either have to leave them misnamed so you have:
.height_900 {
height: 700px;
}
-Or- you have to change the class name everywhere you're using it. That defeats the purpose of CSS.
Instead name your class by what it is supposed to contain. Something like: .menuItem
which allows anyone to know they would be changing the properties of your menu items.
Upvotes: 2
Reputation: 101614
$(<Selector>).attr('class').split('_')[1];
NOTE I don't do any error-checking. And what is it you're trying to do (more explicitly), there's got to be a better way.
Upvotes: 3