How to call split(token) on a string that does not contain the token without causing an error?
Posted
by Vorinowsky
on Stack Overflow
See other posts from Stack Overflow
or by Vorinowsky
Published on 2009-07-20T16:17:59Z
Indexed on
2010/03/28
22:13 UTC
Read the original article
Hit count: 167
I have two types of strings as the IDs of elements in my HTML markup:
Dates:
"april-23"
"march-20"
and season names:
"springtime"
"winter"
The dates have a dash separating the month and the day.
The seasons are a single word with no other tokens.
I want to assign the month or the season to a new variable called:
time_of_year
If I do this:
var time_of_year = $(this).attr("id").split('-')[0];
It will work on the months but if I call it on a season name which does not contain the token, will it generate an error?
What's the safe way to do this?
© Stack Overflow or respective owner