Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to access time from given date.

Here is HTML

<input type="date" name="fromdate" id="fromdate" onchange="displaytimefromdate(this.value)" />
<div id="timepicker"></div>

Here is Javascript Code:

    var availableTime = {"2014-07-18":["Friday, 02:02:00 - 00:00:00","Friday, 01:01:00 - 01:01:00"],"2014-07-19":["Saturday, 02:02:00 - 00:00:00","Saturday, 01:01:00 - 01:01:00"],"2014-07-20":["Sunday, 02:02:00 - 00:00:00","Sunday, 01:01:00 - 01:01:00"]};

function displaytimefromdate(timefromdate) {
        jQuery('#timepicker').html('<div class="form-group"><div class="radio"><label><input class="form-control input-lg bg-darkBlue fg-white" type="radio" id="timepicker" name="timepicker" /></label>'+availableTime.timefromdate+'</div></div>');
}

Here is JsFiddle :- http://jsfiddle.net/rW73e/

Help would be appreciated.

Thanks

share|improve this question
add comment

3 Answers 3

Take a look here:

http://jsfiddle.net/rW73e/2/

I have given a direct string value to timefromdate.

So,

availableTime[timefromdate];

works fine. It has no problem.

Your date object keys are limited to 2 or 3 values which you have included in the availableTime, which map directly to the time. Other dates are not mapped.

If you select other dates which are not present in the availableTime object, it will surely return undefined.

share|improve this answer
add comment

If I understand correctly you are trying to get a value from your json object.

Try:

availableTime[timefromdate] 

instead of:

availableTime.timefromdate

Hope this helps

share|improve this answer
    
jsfiddle.net/rW73e/1 nothing changes –  John Cargo Jul 6 at 9:34
    
availableTime - is a map where "2014-07-18" is a key and a value is array. If you select date whitch doesn't exists in this map - you will always get "Undefined". Select some date whitch exists in your availableTime –  StrekoZ Jul 6 at 9:39
    
that's bracket notation.. which is perfectly good. –  Amit Joki Jul 6 at 9:40
add comment

With bracket notation it's working, however the the object you try to access defined as array.

var availableTime = {"2014-07-18":["Friday, 02:02:00 - 00:00:00","Friday, 01:01:00 - 01:01:00"],"2014-07-19":["Saturday, 02:02:00 - 00:00:00","Saturday, 01:01:00 - 01:01:00"],"2014-07-20":["Sunday, 02:02:00 - 00:00:00","Sunday, 01:01:00 - 01:01:00"]};

function displaytimefromdate(timefromdate) {
    jQuery('#timepicker').html('<div class="form-group"><div class="radio"><label><input class="form-control input-lg bg-darkBlue fg-white" type="radio" id="timepicker" name="timepicker" /></label>'+availableTime[timefromdate]+'</div></div>');
}

http://jsfiddle.net/rW73e/3/

If you want to use the entire block, you should rearrange the object as:

var availableTime = {"2014-07-18":["Friday, 02:02:00 - 00:00:00, Friday, 01:01:00 - 01:01:00"],"2014-07-19":["Saturday, 02:02:00 - 00:00:00, Saturday, 01:01:00 - 01:01:00"],"2014-07-20":["Sunday, 02:02:00 - 00:00:00, Sunday, 01:01:00 - 01:01:00"]};

http://jsfiddle.net/rW73e/4/

... or access the value within the array:

function displaytimefromdate(timefromdate) {
        jQuery('#timepicker').html('<div class="form-group"><div class="radio"><label><input class="form-control input-lg bg-darkBlue fg-white" type="radio" id="timepicker" name="timepicker" /></label>'+availableTime[timefromdate][0]+'</div></div>');

http://jsfiddle.net/rW73e/5/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.