最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - foo.substr gives "Is not a function" error - Stack Overflow

programmeradmin4浏览0评论

I'm using Jquery's datepicker plugin (which works fine). Then I need to extract the "day of the week" from the picked date.

When using foo.substring(0,3) to assign the first three characters of the datepicker('getDate') I get: TypeError foo.substr is not a function.

$(function () {
    $("#textDatepicker").datepicker();
});

function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example)..... 
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.substr(0, 3)
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}

<head>
<script src=".min.js" type="text/javascript"></script>
<script src=".10.3/jquery-ui.js"></script>
<script src=".js" type="text/javascript"></script>
<link rel="stylesheet" href=".10.3/themes/smoothness/jquery-ui.css"/>
</head>
<body>
Select Date:&nbsp;<input type="text" id="textDatepicker" onchange="selectedDay();">
<br><br>
<span id="dayofweek">Selected day of week replaces this</span>
</body>

I have also pasted at: jsfiddle

Any help would be appreciated.. Thanks in advance...

I'm using Jquery's datepicker plugin (which works fine). Then I need to extract the "day of the week" from the picked date.

When using foo.substring(0,3) to assign the first three characters of the datepicker('getDate') I get: TypeError foo.substr is not a function.

$(function () {
    $("#textDatepicker").datepicker();
});

function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example)..... 
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.substr(0, 3)
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}

<head>
<script src="http://code.jquery./jquery-latest.min.js" type="text/javascript"></script>
<script src="http://code.jquery./ui/1.10.3/jquery-ui.js"></script>
<script src="https://jquery-blog-js.googlecode./files/SetCase.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://code.jquery./ui/1.10.3/themes/smoothness/jquery-ui.css"/>
</head>
<body>
Select Date:&nbsp;<input type="text" id="textDatepicker" onchange="selectedDay();">
<br><br>
<span id="dayofweek">Selected day of week replaces this</span>
</body>

I have also pasted at: jsfiddle

Any help would be appreciated.. Thanks in advance...

Share Improve this question edited Jul 18, 2013 at 23:36 Sergio 28.8k11 gold badges89 silver badges132 bronze badges asked Jul 18, 2013 at 23:29 ClayClay 531 gold badge3 silver badges11 bronze badges 6
  • 2 Why do you load both jQuery 1.9.1 and 1.10.2 (latest.min.js)? – Sergio Commented Jul 18, 2013 at 23:30
  • Are you 100% sure that it's a string, but not an object, that you're trying to take the substring of? – Jonast92 Commented Jul 18, 2013 at 23:32
  • My guess would be that .substr only works on strings and you are trying to work on a jQuery object. – Nathan Lippi Commented Jul 18, 2013 at 23:33
  • Haven't work hardly anything using jquery. Thanks for pointing things out. I'll need to workout how to get the value in to a variable. – Clay Commented Jul 18, 2013 at 23:38
  • 1 Um, the documentation says that getDate returns a Date, not a string. – Raymond Chen Commented Jul 18, 2013 at 23:39
 |  Show 1 more ment

5 Answers 5

Reset to default 7

Use

var weekday = foo.toString().substr(0, 3);

DEMO.

var foo = $("#textDatepicker").datepicker('getDate');

returns a Date object, not a string, and has no method substr()

FIDDLE

You can solve it by removing that unsightly inline event handler and doing :

$("#textDatepicker").datepicker({
    onSelect: function() {
        var date = $(this).datepicker('getDate');
        var day  = $.datepicker.formatDate('DD', date);
        $('#dayofweek').html(day);
    }
});

FIDDLE

$("#textDatepicker").datepicker('getDate');

is an object. You can't take the substring of an object with substr.

foo.substr(0,3) is the culprit. It returns a date object and you can do substr on a date object. You could fix the problem by using the code below

function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example)..... 
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.getDay();
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}

called the toString(); method before calling the subString(); like foo.toString().subString(start,length); Worked for me

发布评论

评论列表(0)

  1. 暂无评论