Nomad
Nomad

Reputation: 781

Date coming as undefined with javascript

       function duplicateWithLines()
        {
          var datefield = jQuery("#lineItemDate").val();
          if ( !( datefield && datefield.match( /^\d{1,2}[/]\d{1,2}[/]\d{4}$/ ) ) ) alert("Date is required");
          else {
           //DO blah blah
          }
        }
    ---
    ---

          <input type='button' class='duplicate_button' name='duplicateWithLines' value='Duplicate Line Items:' onClick='duplicateWithLines()'/>
          <hsi:calendar name="lineItemDate" size='10' maxlength='10' />

At Line var datefield = jQuery("#lineItemDate").val(); value is coming as undefined I think. Because of that it failing to enter else block.if condition is satisfying so alert msg printed. But it has to enter else block. And there date format would be entered as dd/mm/yyyy. What is the way to get in to else block.

Upvotes: 0

Views: 602

Answers (2)

KooiInc
KooiInc

Reputation: 122986

Addressing the val()-problem: you are trying to find an element with id lineItemDate. But your element has no id. It does have a name, so $('[name=lineItemDate]').val() should work.

If the formatting is fixed (dd/mm/yyyy), try modifiyng the if part this way to avoid the ugly and expensive matching:

var dateFromValue = new Date(
                     $('[name=lineItemDate]').val().split('/').reverse().join('-')
                    );
if (!isnNaN(dateFromValue)) {
   /* it's a date! */
} else {
  /* don't bother */
}

While we're at it, don't use inline handlers. Every activation of an inline handler spawns a new javascript interpreter. In your scripting use:

$('[name=duplicateWithLines]').on('click', duplicateWithLines);

It may be wise to rename your function, to avoid name clashes.

Upvotes: 1

Jason
Jason

Reputation: 15931

jQuery doesn't know how to retrieve the value from the hsi:calendar element. You need to find the html element that contains the data value in order to use jquery to fetch it.

Upvotes: 0

Related Questions