val() is not a function when looping through elements

I want to bind a value but I am getting the error below.

val() is not a function.

Please help me.

$(function() {
  $('.pic').each(function() {
    $(this).datepicker({
      dateFormat: "yy-mm-dd",
      onSelect: function() {
        var dateObject = $(this).datepicker('getDate');
        var date=$.datepicker.formatDate('yy-mm-dd', dateObject);
        $(this).attr('id').val(date);
      }
    });

Answers 1

  • val is not a function of attr. You can try this:

    $(function() {
      $('.pic').each(function(){
        $(this).datepicker({
          dateFormat: "yy-mm-dd",
          onSelect: function() {
            var dateObject = $(this).datepicker('getDate');
            var date=$.datepicker.formatDate('yy-mm-dd', dateObject);
            $(this).attr('id',date)
          }
        });
    

Related Articles