Jump to content

Shifting Form Field Values onclick


millsy007

Recommended Posts

I have a page from which the user can select a date (in a text box updated via a calendar) and a time (selected from an options list)

I have two links that let the user cycle through the date field.

 

<a href="#" onclick="shiftDate(1, 'down');">Previous</a>
<a href="#" onclick="shiftDate(1, 'up');">Next</a>

 

My javascript then changes the date accordingly

 

function shiftDate(days, up_or_down)
{
date = new Date(document.getElementById('date').value);

if (up_or_down == 'up') {
date.setDate(date.getDate() + days);
} else {
date.setDate(date.getDate() - days);
}

document.getElementById('date').value = (date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear();
GetSchedule();
return false;
}

 

This works fine but i need to incorporate the time option too. The time dropdown list has:

 

<select name="time" id="time" style="width:150px">
      <option>Select Time</option>
      <option value="9:00">Morning</option>
      <option value="12:00">Afternoon</option>
      <option value="16:00">Evening</option>
      <option value="22:00">Late</option>
</select> 

 

What I would like to happen is that when the user clicks the 'next' it firstly updates the time, moving on one option value in the time field, and then only if the option value is <option value="22:00">Late</option> shifting/moving on 1 day.

 

Is this possible? I cant seem to get this functionality working

Link to comment
https://forums.phpfreaks.com/topic/146744-shifting-form-field-values-onclick/
Share on other sites

Here's a generic function that you could use for multiple things:

<html>
<head>

<script>

function moveSelectOpt(selID, step)
{
    var selObj = document.getElementById(selID);
    var newID = (selObj.selectedIndex + step);

    if (newID>=1 && newID<=(selObj.options.length-1))
    {
        selObj.selectedIndex = newID;
    }
    else
    {
        selObj.selectedIndex = (newID<=1) ? (selObj.options.length-1): 1 ;
    }

    return;
}

</script>

</head>

<body>

<select name="time" id="time" style="width:150px">
      <option>Select Time</option>
      <option value="9:00">Morning</option>
      <option value="12:00">Afternoon</option>
      <option value="16:00">Evening</option>
      <option value="22:00">Late</option>
</select><br>
<a href="#" onclick="moveSelectOpt('time', 1);">Next Time</a>
<a href="#" onclick="moveSelectOpt('time', -1);">Previous Time</a>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.