0

This the code in the var named available_dates

var available_dates = ["08-11-2018","09-11-2018"]

I wonder how can I choose the second date of the var and selected, to my second code

document.getElementById('app_date').value = available_dates

Taplar
  • 24,788
  • 4
  • 22
  • 35
  • 1
    Are you asking how to perform array index access? – Taplar Feb 17 '20 at 17:46
  • 2
    `available_dates[1]` – Leandro Matilla Feb 17 '20 at 17:47
  • I can't even find a suitable duplicate of this. – GrafiCode Feb 17 '20 at 17:48
  • 1
    https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json is a duplicate, but so is google. – Taplar Feb 17 '20 at 17:49
  • @Taplar that's far too complicated for this question... something like this is more appropriate in my opinion: https://stackoverflow.com/questions/8238456/how-to-get-value-at-a-specific-index-of-array-in-javascript – GrafiCode Feb 17 '20 at 17:52
  • Yeah, I would agree. – Taplar Feb 17 '20 at 17:53
  • Does this answer your question? [Get the last item in an array](https://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array) – Triby Feb 17 '20 at 18:12
  • Does this answer your question? [How to get value at a specific index of array In JavaScript?](https://stackoverflow.com/questions/8238456/how-to-get-value-at-a-specific-index-of-array-in-javascript) – Taplar Feb 17 '20 at 18:31

3 Answers3

0

You can simply access a value in your array available_dates by doing this:

document.getElementById('app_date').value = available_dates[i]

Be careful to the fact that indexes start from 0. so available_dates[0] will return the first index. available_dates[1] will return the second and so on.

0

U must indicate which position of the array u want:

document.getElementById('app_date').value = available_dates[1]

Be happy!!

0

Simply access array by its key available_dates[1]

function getDate() {
  var available_dates = ["08-11-2018","09-11-2018"]  
  document.getElementById("myText").value = available_dates[1];
}
<input type="text" id="myText" value="">

<button onclick="getDate()">Get Date</button>

Link for javascript array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
  • Thanks ....but what if var available_dates was in another script not in the same function , how to detecte the value !! – ADDA BENDADI Feb 17 '20 at 18:52
  • hi see in any language if you want to access a variable it should be declared before it's use so if the variable is in another script file than include that file before this function is being called and than use that variable – Passionate Coder Feb 18 '20 at 04:37