I'm trying to run function getOrderData number of times that user selected in the dropdown, how can I do that?
Jade file:
p
strong Number of pieces:
select(id='numOfPieces')
Javascript file:
function getOrderData() {
//this just creates the dropdown dynamically so I could get number that user selected by index value
const dropdown = document.getElementById("numOfPieces");
var options = ["1", "2", "3", "4", "5"];
for(var i = 0; i < options.length; i ) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
dropdown.appendChild(el);
}
//checks selected number
let times = dropdown.options[dropdown.selectedIndex].value;
}
$("#yes").click(function () {
getOrderData();
}
CodePudding user response:
function getOrderData() {
alert('Hallo!!');
}
$("#yes").click(function () {
let value = $('#numOfPieces').val();
value = value && value>0 ? parseInt(value) : 1;
for (let i = 0; i < value; i ) {
getOrderData();
}
});
CodePudding user response:
You can use the times()
method from 30 seconds of code. Here is the code from that website, plus an example:
const times = (n, fn, context = undefined) => {
let i = 0;
while (fn.call(context, i) !== false && i < n) {}
};
times(5, i => console.log(`Called ${ i } times.`));