Momo Camping
Momo Camping

Reputation: 43

substring does not work with jquery .text()

My code:

monthName = $("#StartDateMonth option:selected").text();
console.log(monthName, monthName.substring(0,3), 'December'.substring(0,3));

Return:

December
<nothing>
Dec

Why this ridiculous thing occur? My browser: Chrome - Ubuntu

Upvotes: 1

Views: 2237

Answers (1)

user1902830
user1902830

Reputation:

It must be the spaces in the result of .text(). You can trim it before calling substring.

Example:

monthName = $.trim($("#StartDateMonth option:selected").text());
console.log(monthName, monthName.substring(0,3), 'December'.substring(0,3));

Upvotes: 1

Related Questions