ToddN
ToddN

Reputation: 2961

jquery create inputs and array name of inputs

One types the number of Inputs they want and click GO, it creates that many number of input fields. How do I make it so each will have their own unique name?

I want to have unique names for each input created so ie:

FYI I'm also using PhP if it is easier to be incorporated

Upvotes: 1

Views: 1216

Answers (1)

Naftali
Naftali

Reputation: 146320

You can what is on this fiddle:

$(document).ready(function(){
        var numberOfInputs = 0;
        $('#addButton').click(function(){

          var count = parseInt($('#quantity').val());
          var newHTML = [];
          for(var i=0;i<count;i++){
            var html = $('Product Code: <input name="productcode" type="text"/> &nbsp;&nbsp;Description: <input name="desc" type="text"/>&nbsp;&nbsp; $<input width="100px" type="text" name="dollars" size="5" maxlength="6"/>.<input width="50px" type="text" name="cents" size="1" maxlength="2"/><br/>');
            $(html[0]).attr('name',$(html[0]).attr('name')+numberOfInputs);
            $(html[2]).attr('name',$(html[2]).attr('name')+numberOfInputs);
            $(html[4]).attr('name',$(html[4]).attr('name')+numberOfInputs);
            $(html[6]).attr('name',$(html[6]).attr('name')+numberOfInputs);
            numberOfInputs++
            newHTML.push(html);
            $('#sandbox').append(html);
          }

        });
  });

Upvotes: 0

Related Questions