My problem is fairly simple to explain yet a little tricky to implement. Logically it seems very possible but im a little stuck.
Objective/purpose:
I have my first SELECT named “hour” and a second SELECT named “rec_hour”. Each have the same values. See HTML code below:
<select id="hour">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<!-- This would go up to 23 -->
</select>
<select id="rec_hour">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<!-- This would go up to 23 -->
</select>
I receive a variable from a PHP file “$hours” which contains a integer value ranging from 0 to 24. Now, I want to get that value of hours, lets say its 6 for this scenerio, and subtract this value in the second SELECT (rec_hour).
So in essence, if my returned variable is 6. and i choose say 10 from the first select (hour) then i want the second select to show/select the value that is 10-6 which is 4.
I hope that makes sense..
I have tried the script below out of desperation but to no avail:
$(document).ready(function() {
$("#hour").change(function() {
var myHour = $(this).val();
var Subtra = 10;
var recHour = myHour - Subtra;
$("#rec_hour").val = recHour;
$("#rec_hour").text(recHour);
}
});
});
My code above im using “Subtra” as a litmus test value.. this would actually change based on my php result.
UPDATE:
All i really want to do is this..
I receive a time (hours only) say 2 -> refers to 2am (14 would be 2pm) n then my php will calculate hours to find out how long a journy will take. For instance say tht is 6 hours.. then i want to show the user 6 hours behind from 2am..which is 8pm.. i.e. you should leave home at 8pm.. you follow? or am i confusing?
Set the value of rec_hour using .val(). Also note there was a few syntax errors in your code. You had an extra curly brace. Also you want to handle the case where subtra > myHour, using the abs value function.
Corrected Code:
Original With Errors Noted:
Demo: http://jsfiddle.net/FdW4w/3/
I gave some more descriptive variables names and applied the math. The fiddle is also updated to reflect this.