Generating a Javascript id in a loop
Date : March 29 2020, 07:55 AM
With these it helps Due to the closure created by your anonymous functions you always have the same frm/tu (should be to btw.) variable. By introducing a self-calling anonymous function which receives those variables as parameters you create new ones avoiding this issue: $(function () {
var frm = 0;
var tu = 0;
var i = 0;
for (i = 1; i <= 5; i++) {
frm = "#from" + i;
tu = "#to" + i;
(function (frm, tu) {
$(frm).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yy/mm/dd',
maxDate: '-1',
onSelect: function (selected) {
$(tu).datepicker("option", "minDate", selected)
}
});
$(tu).datepicker({
dateFormat: 'yy/mm/dd',
changeMonth: true,
changeYear: true,
maxDate: '-1',
onSelect: function (selected) {
$(frm).datepicker("option", "maxDate", selected)
}
});
})(frm, tu);
}
});
|
Creating monsters with a for loop generating id's
Tag : chash , By : OllieDoodle
Date : March 29 2020, 07:55 AM
Any of those help I am new to game programming and C#, but I have some programming experience from javaScript and PHP. aMonster[] bigMonster = new aMonster[51];
for (int i = 0; i < 50; i+=2)
{
bigMonster[i] = new aMonster();
bigMonster[i].id = i;
bigMonster[i].name = "Gorky";
bigMonster[i].race = "Orc";
bigMonster[i].age = 320;
bigMonster[i].health = 200;
bigMonster[i+1] = new aMonster();
bigMonster[i+1].id = i;
bigMonster[i+1].name = "Runathu";
bigMonster[i+1].race = "Shaman";
bigMonster[i+1].age = 670;
bigMonster[i+1].health = 100;
}
var bigMonster = new List<aMonster>();
var id = 0;
for(int i=0; i<30; i++)
{
bigMonster.Add(new aMonster { id=id++,name="Gorky",race="Orc",age=320,health=200 });
bigMonster.Add(new aMonster { id=id++,name="Runathu",race="Shaman",age=320,health=200 });
//and so on
}
|
creating multiple button using while loop but only first button is responsive when using .onclick function
Date : March 29 2020, 07:55 AM
I hope this helps you . that's because you're creating number of buttons with the same id and that is invalid because id must be unique. instead assign name to like buttons. <input type='button' id='like1' name='like' value='like' />
var likeBut= document.getElementsByName("like");
for(var i=0;i<likeBut.length;i++){
likeBut[i].onclick = function(){
likemsg(HTTP);
}
}
|
Generating variables in a for loop javascript
Date : March 29 2020, 07:55 AM
hop of those help? You could create an array of the variable names, then loop through them using the window object to build the assignments: var ary = ['vertBar_1', 'vertBar_2', 'vertBar_3'];
for (var i = 0; i < ary.length; i++) {
window[ary[i]] = document.getElementById(ary[i]);
}
console.log(vertBar_1, vertBar_2, vertBar_3)
|
How to prevent checkbox loop and radio button loop from creating two separate price totals
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Just have one event handler and one loop If you only have data-price on the elements you need to loop, then you can change [...document.querySelectorAll('input[data-price][type=checkbox], input[data-price][type=radio]')].forEach(function(box) {
[...document.querySelectorAll('input[data-price]')].forEach(function(box) {
const form = document.getElementById('bookingForm');
const total = document.getElementById('total');
form.addEventListener("click", function(e) {
if (e.target.name === "event[]" || e.target.name === "delivery") {
let totalprice = 0;
[...document.querySelectorAll('input[data-price][type=checkbox], input[data-price][type=radio]')].forEach(function(box) {
if (box.checked) {
totalprice += +box.dataset.price;
}
})
document.querySelector("[name=total]").value = totalprice.toFixed(2);
}
})
<form id="bookingForm" action="javascript:alert('form submitted');" method="get">
<section id="bookEvents">
<h2>Select Events</h2>
<div class='item'>
<span class='eventTitle'>Event number 1</span>
<span class='eventPrice'>10.50</span>
<span class='chosen'><input type='checkbox' name='event[]' value='1' data-price='10.50'></span>
</div>
<div class='item'>
<span class='eventTitle'>Event number 2</span>
<span class='eventPrice'>5.00</span>
<span class='chosen'><input type='checkbox' name='event[]' value='2' data-price='5.00'></span>
</div>
<section id="Cost">
<input type="radio" name="delivery" value="ticket" data-price="10">
<h2>Total Price</h2>
Total Price <input type="text" name="total" size="12">
</section>
<p><input type="submit" name="submit" value="Book"></p>
</section>
</form>
|