I have a div of list of words displayed in textboxes
<div id = "rightbox" ondrop="drop(event)" ondragover="allowDrop(event)">
<div><input type="text" id="appleword" value="apple" class="textbox" readonly="ture" draggable="true" ondragstart="drag(event)"></div>
<div><input type="text" id="orangeword" value="orange" class="textbox" readonly="ture" draggable="true" ondragstart="drag(event)"></div>
<div><input type="text" id="peachword" value="peach" class="textbox" readonly="ture" draggable="true" ondragstart="drag(event)"></div>
</div>
Needs help on creating similar text boxes(in array) dynamically with different values.
var words = ['apple', 'orange', 'peach'], // add more to array if needed
newInputs = document.createDocumentFragment(); // fragment to collect new inputs
// Loop through array of words and generate inputs
words.forEach(function (word) {
var wrapper = document.createElement('div'),
fieldSet = document.createElement('fieldset'),
input = document.createElement('input'); // Inputs default to type=text
// Decorate elements with needed attributes here (abbreviated)
wrapper.id = word + 'word';
input.id = word + 'input';
input.setAttribute('value', word);
input.setAttribute('class', 'textbox');
input.readOnly = true;
// Nest all these elements and add them to the fragment
newInputs.appendChild(wrapper).appendChild(fieldSet).appendChild(input);
});
// Insert the fragment into the DOM
document.getElementById('rightbox').appendChild(newInputs)
Adjust code as needed to add missing attributes and event handlers. If you have to support anything older than IE9 change the forEach
loop to a for
loop.
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.