简体   繁体   中英

How do I assign an array to an HTML form input value?

I would like to assign a value of an array to an HTML input element.

In my browser console, the following code works:

let between0400am0800am = [1669352400000, 1669438800000];
document.getElementById("time400am800amEveryDay").value = between0400am0800am;

However, in my code, the same code doesn't seem to work.

Find below my html code:

<div class="form-check">
   <input class="form-check-input checkbox" type="checkbox" id="time400am800amEveryDay">
</div>

Naturally, the desired outcome is:

<div class="form-check">
   <input class="form-check-input checkbox" type="checkbox" value="1669352400000,1669438800000" id="time400am800amEveryDay">
</div>

Just use the Array.prototype.join() function. You can pass an parameter which will inserted between the array items.

let between0400am0800am = [1669352400000, 1669438800000];
document.getElementById("time400am800amEveryDay").value = between0400am0800am.join(",");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM