Grady D
Grady D

Reputation: 2019

unable to get value from <input>

I have multiple <input class="form-control inputDate" type="text" value="12/31/1899 7:00 PM"> on a page. I also have the following javascript,

var inputs = jQuery('.inputDate');
for (var i = 0; i < inputs.length; i++) {
  console.log("test");
  jQuery(inputs[i]).val().split('/')[2];
  var input = jQuery(inputs[i]).val().split('/')[2];
  console.log("TEST" + input);
  if (input.length > 4) {
    input = input.split(" ")[0];
  }
  if (input < '2015') {
    jQuery(inputs[i]).val("");
  }
}

The problem is that var input does not have a value. I have tried opening up the console and testing (against the html from above) and nothing is getting the value.

I have tried,

jQuery(inputs[i]).val();
jQuery(inputs[0]).val();
document.getElementsByClassName("inputDate")[0].value;

But nothing returns the value. I think part of the issue is that the value is set in the backend to 1899 instead of starting off null.

How can I get the value?

EDIT-------------------------------------

I have done more testing and when I do

var test = inputs[0];
jQuery(test).val("123")

The HTML markup still shows the date. But if I do (from IntheWeeds)

jQuery(".inputDate").each(function() {
   console.log($(this).val());
});

it returns 123 and "" even though the value in the HTML markup is still a date.

Upvotes: 0

Views: 781

Answers (3)

Nishanth Matha
Nishanth Matha

Reputation: 6081

You are not accessing the value attribute there. try

 var test = inputs[i];
 jQuery(test).val();

or use .each method:

var inputs = jQuery('.inputDate');
jQuery(inputs).each( 
  jQuery(this).val().split('/')[2];
  // rest of the code don't forget to get rid of ending '}'

Upvotes: 2

intheweeds
intheweeds

Reputation: 79

I definitely would recommend using "each", as noob described in his updated answer. Here is a simplified example showing how to log each textbox value:

$(".inputDate").each(function() {
   console.log($(this).val());
});

Upvotes: 1

Glenn Ferrie
Glenn Ferrie

Reputation: 10390

Try this:

var inputs = $('.inputDate').val();

// Expect: input = "12/31/1899 7:00 PM"

Upvotes: 0

Related Questions