Creating a button in Javascript for each value generating in the loop

Creating a button in Javascript for each value generating in the loop

Content Index :

Creating a button in Javascript for each value generating in the loop
Tag : javascript , By : PeteFilicetti
Date : November 29 2020, 12:01 PM

help you fix your problem I have a data from the back end and I have to create a button for each value generated by that time , You can use
var button = document.createElement("button");
button.setAttribute("id", 'btn_' + i);
var btn_text = document.createTextNode(values[i].good + ' - ' + values[i].bad);
var element = document.getElementById("div1") //If have wraper
var element = document.body // If no

//Then Apply
var values = [{
  good: 13,
  bad: 14
}, {
  good: 14,
  bad: 15
}, {
  good: 16,
  bad: 17

//element = document.getElementById("div1") //If have wraper
var element = document.body
for (var i in values) {

  var para = document.createElement("button");
  var node = document.createTextNode(values[i].good + ' - ' + values[i].bad);
  para.setAttribute("id", 'btn_' + i);

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Generating a Javascript id in a loop

Tag : javascript , By : Eric
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) {
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy/mm/dd',
                maxDate: '-1',
                onSelect: function (selected) {
                    $(tu).datepicker("option", "minDate", selected)
                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

Tag : javascript , By : Heals1ic
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(){

Generating variables in a for loop javascript

Tag : javascript , By : n800s
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

Tag : javascript , By : phil
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 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>
    <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">

    <p><input type="submit" name="submit" value="Book"></p>
Related Posts Related QUESTIONS :
  • MAC addresses in JavaScript
  • Capturing TAB key in text box
  • How to set CSS background color of HTML using JavaScript
  • How can I make the browser see CSS and Javascript changes?
  • redux how to use state
  • All data in the db is lost when closing a express based node js server
  • How to convert javascript code for Angular
  • XPath select on child nodes
  • Idle event on mouse move - how to stop script permanently on mouse move
  • Why do I get logic is not defined in this code
  • When I enter a number in the input, the first function starts
  • ES6 imports and node_modules hell
  • How to get css selector of clicked-on element?
  • How to auto input MySQL data (input column 1 data to auto input column 2 data with same column) in php?
  • Appending to Datalist Via Javascript
  • Customizing Material-UI Components with Styled-Components
  • Populate elements with class name using Javascript
  • How to execute promises in batches (first resolving 10, then next 10 and so on)
  • Maximo JS automation script: "importPackage" is not defined
  • How to surround selected text inside textarea with <em> on key combination CTRL+i?
  • Your configured registry does not support audit requests ~ On npm audit
  • What's the Angular equivalent of Vue <slot/>?
  • Iconify icon not updating when "data-icon" set with setAttribute()
  • Javascript newbie stuck with XMLHttpRequest return data
  • How to add obj to to state in my example Redux React
  • Loading "grunt-karma.js" tasks...ERROR >> SyntaxError: Unexpected identifier
  • Why do I get log is not defined
  • get the multiple index of the same string
  • How can I iterate over an Array object by using For IN or FOR OF
  • Launch local executable from web browser
  • How to prepare array of json objects to d3 hierarchical tree structure
  • VueJS - event emitted - show HTML before other calculations
  • Export SASS/SCSS variables to Javascript without exporting them to CSS
  • How to attach an event handler only once continuously
  • How to make button open a link in new tab in JavaScript?
  • Find range from the javascript object
  • How can I use my jQuery code as JavaScript?
  • How to show 5 images in a 2 row slider with bxslider
  • How to convert stream into variables in typescript
  • Three.js custom shader error on Chrome GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs
  • My JavaScript doesn't work. How can I solve this problem?
  • Why is this async function is running synchronously?
  • Get updated variable from within react hook before return
  • I want to show all image preview but whenever I upload a new one the old one is gone
  • Would I be able to give access to specific electron APIs safely?
  • Why several clicks fire for each instance of a Class?
  • cant grab value from div using attr()
  • Make web service not accessible by browser?
  • Select DOM elements within template
  • How to customize angular ui-grid row background color?
  • Binding data in two respective $scope in AngularJS, how?
  • react put some props after import class
  • Variable is distributed among same components
  • switch class in jQuery/JavaScript for active/inactive state
  • How To Track Outbound Clicks Using Javascript
  • How to get total seconds elapsed since the beginning of the month?
  • Protractor: Stale element reference at "browser.wait(EC.visibilityOf(confDial), FIFTY_SECONDS);"
  • Access to an array inside an object
  • Reseting an JavaScript Interval with different value?
  • Learning Angular for the first time - version 2 or 4?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com