Dynamically assign ID

后端 未结 1 761
庸人自扰
庸人自扰 2021-01-28 06:52

I am using nodejs and expressjs.

I load a list from a database. Selecting any of the list elements bring up an overlay with to EDIT and DELETE the selected item. I can

相关标签:
1条回答
  • 2021-01-28 07:02

    Try this:

    var box     = document.getElementById('box'),
        buttons = box.getElementsByTagName('button');
    
    for(var i = 0; i < buttons.length; i++){
        buttons[i].id = [ 'my', 'button', 'id', i ].join('-');
    }
    

    JsFiddle

    Also, you may try to "map" actions:

    var box     = document.getElementById('box'),
        buttons = box.getElementsByTagName('button');
    
    var map = [
        'editcustomerform',
        'deletecustomer',
        'emailcustomer',
        'callcustomer',
        'gpstocustomer',
        'createquote',
        'createsalesorder',
        'createcashsale'
    ];
    
    for(var i = 0; i < buttons.length; i++){
        buttons[i].id = [ 'my', 'button', 'id', i ].join('-');
        buttons[i].onclick = function(){ location.href = [ '/scape', map[i], 'id' ].join('/'); }
    }
    
    0 讨论(0)
提交回复
热议问题