javascripthtmlsfdc

Set Dynamic Id using JS using Counter


I am trying to dynamically set a cell id using a cnt counter variable. However, instead of a number, I get the cnt word when viewing page source in browser.

What is wrong

 q1TargetVolume.setAttribute('id', `q1TargetVolume-${cnt}`);  

Solution

  • I guess you wish to append a count value to the string q1TargetVolume-, and use it as the id for an element. You can try this method.

    q1TargetVolume.setAttribute( 'id', 'q1TargetVolume-' + cnt );//cnt is the count value you wish to append