user3271762
user3271762

Reputation: 23

How to set attribute for id with increment option and to start increment with specific number

Here is my Html part

 <input type="text" class="demo" id="" value="" />
 <input type="text" class="demo" id="" value="" />
 <input type="text" class="demo" id="" value="" />
 <input type="text" class="demo" id="" value="" />
 <input type="text" class="demo" id="" value="" />

My jquery

 $('.demo').attr('id', function (i) {
    return 'Sample_' + (i + 1);
 });

Now I need this "i" to start from 5 rather than 0 so my result will be starting from Sample_6, Sample_7 etc..

How can i do that???

Upvotes: 1

Views: 212

Answers (2)

CodeSkiller
CodeSkiller

Reputation: 1

Simply:

 var a = 5;
 $('.demo').attr('id', function (i) {
    if(i == 0) {
        a++;
        return 'Sample_' + (a);
    }
    else
        return 'Sample_' + (i + 1);
 });

Upvotes: 0

David Thomas
David Thomas

Reputation: 253318

I'd suggest:

$('.demo').attr('id', function (i) {
    return 'Sample_' + (i + 5);
});

JS Fiddle demo.

Upvotes: 1

Related Questions