0

I am trying to assign a unique Id to my button which is created dynamically this way

This is my code

for (var i = 0; i < response.length; i++) {

    var uniqueNumber = new Date().getTime();

            var $ordernow = $('<input/>').attr({
                type: 'button',
                name: 'btn1',
                class: 'ordernow btn btn-' + classstyle + '',
                value: 'Order Now',
                id : uniqueNumber,
                style: 'float:right'
            });

    }

But sometimes i observe that the id that is being genertaed is sometimes same (However this is not happening every time) as shown above

This is the HTML that is generated for the above

<div class="inner-intit">
   <input type="button" name="btn1" class="ordernow btn btn-success" value="Order Now" id="1412689973307" style="float:right">
   <input type="button" name="btn1" class="ordernow btn btn-success" value="Order Now" id="1412689973307" style="float:right">
</div>

Is there any solution how to recitify this ??

Just for additional information this is how the UI looks for my screen

enter image description here

Pawan
  • 31,545
  • 102
  • 256
  • 434

2 Answers2

0

You could add a random string after the timestamp. With an alpha numeric string of 6 or more characters, plus the timestamp, the chance of overlap is incredibly unlikely.

However, you may rethink the approach you are taking. You probably don't need a front-end generated unique ID.

Jason
  • 4,079
  • 4
  • 22
  • 32
0

You can use the variable i and add it to uniqueNumber

id : uniqueNumber + i,
Anton
  • 32,245
  • 5
  • 44
  • 54