View Full Version : Fill field with day of week field upon user selected date.

05-15-2012, 10:14 PM
Hi folks.

Looking to see if anyone could tell me where I'm going wrong with this script...

I'm trying to have a field automatically filled with the day of the week ("Monday", "Tuesday", "Wednesday" and so on), upon the user selecting a date from the datepicker jQuery. If this isn't clear, I'll clarify further down the page.

Here is the script:

<script type="text/javascript">
var d = $('input[name="item_meta[428]"]').val();
var n = new Date(d).getDay();
if(n == 0)
val v = 'Sunday';
else if(n == 1)
val v = 'Monday';
else if(n == 2)
val v = 'Tuesday';
else if(n == 3)
val v = 'Wednesday';
else if(n == 4)
val v = 'Thursday';
else if(n == 5)
val v = 'Friday';
else if(n == 6)
val v = 'Saturday';

I'm basically trying to say, if the user selected today (15/05/2012) in the field 428 it would fill the field 429 with "Tuesday".

Again, if the user selected 18/05/2012 in the field 428 then it would automatically fill field 429 with "Friday".

It's being done to work in conjunction with a wordpress plugin called Formidable Pro hence the item_meta[428] etc.

Any assistance would be greatly appreciated.


Old Pedant
05-16-2012, 12:13 AM
Well, I don't use jQuery, but I don't know why the following won't work:

var d = $('input[name="item_meta[428]"]').value;
var wdays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
$('input[name="item_meta[429]"]').value = wdays[(new Date(d)).getDay()];

Instead of using jQuery's val() function, why not use the .value property for the <form> field??? Surely that's what val( ) is doing, under the covers.