I have the following code:
/
I'm pletely new to javascript, this is my first time using it. The values of the HTML selects MUST be 01,02 etc, that's why I had to use a big long if else statement. The values have to be submitted to an application on a server, which is extremely fussy about what way it takes in values.
Why won't it set the day as 15 (today) in the select box?
I have the following code:
http://jsfiddle/SPWWx/
I'm pletely new to javascript, this is my first time using it. The values of the HTML selects MUST be 01,02 etc, that's why I had to use a big long if else statement. The values have to be submitted to an application on a server, which is extremely fussy about what way it takes in values.
Why won't it set the day as 15 (today) in the select box?
Share Improve this question edited Jul 15, 2010 at 11:10 Marcus asked Jul 15, 2010 at 11:04 MarcusMarcus 31 silver badge3 bronze badges2 Answers
Reset to default 7You have a few issues, you're not including jQuery on the left, the element has a name not an ID or CID
, so it needs to be id="CID"
or your selector needs to be select[name='CID']
. Last, you need to pass a string to .val()
to get the result you want, otherwise it's trying to set it to "4"
, which doesn't equal "04"
.
You can shorten all your code down to this though:
var day = new Date().getDate().toString();
$("#CID").val(day.length == 1 ? "0" + day : day);
You can test it here, also as Jamiec points out, you want .getDate()
to get the date of the month as opposed to .getDay()
which is of the week.
because the element's name is CID, not it's id! $('#CID') selects the element with the id CID.