1

I seem to be locating answers here and other locations that are close to what I need -- so my apologies if I've duplicated without noticing. And I'm certain that what I've been locating can work...if I knew how to use it :)

I have 4 radio buttons on a page. They have values as 4, 7, 14, 30.

<input type="radio" id="radio1" name="length" value="4"/><label for="radio1">4 DAYS</label>
<input type="radio" id="radio2" name="length" value="7"/><label for="radio2">7 DAYS</label>
<input type="radio" id="radio3" name="length" value="14"/><label for="radio3">14 DAYS</label>
<input type="radio" id="radio4" name="length" value="30"/><label for="radio4">30 DAYS</label>

A return date is needed based on the above selection. Say if radio2 is selected, the return date will be the date selected + 7 days.

Here http://jsfiddle.net/JKGvD/525/ is a great example of this functionality however you can see I'm attempting to manipulate the 'nights' field with user input from above.

I attempted to use the $('input:radio[name=length]:checked').val(); in that Fiddle however it fails with Nan (Not a Number?) in the field.

Please alert me if any further information is needed. And thank you in advance everyone!

camelCase
  • 5,460
  • 3
  • 34
  • 37

2 Answers2

1
function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}

per

How to add 30 minutes to a JavaScript Date object?

Converting your days to minutes we have 1 day = 24 hours = 1440 minutes.

So if you want it in days change the constant accordingly by multiplying by 1440.

To get the radio button value use

$('input[name=radioName]:checked', '#myForm').val()

per

How can I know which radio button is selected via jQuery?

Community
  • 1
  • 1
employee-0
  • 1,023
  • 1
  • 9
  • 19
1

Try casting it to a number, like:

+$('input:radio[name=length]:checked').val();

Remember that HTML values are Strings.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • Ah yes, this was the issue. I was not adding this line in the correct area! I've update the Fiddle [link](http://jsfiddle.net/JKGvD/525/) (this time it's the right link, my apologies above). The value added is slightly off but I'll look into this. Thanks all! – camelCase Jul 16 '13 at 23:39