Don
Don

Reputation: 141

Get dynamic id of textbox inside jquery

My Dynamically created textbox ids

$.each(vendor_name, function(p, tt) { 
    tr.append($("<td/>").html('<input type="text" name="cmpny_id" id="cmpny_id"'+p+' value="'+vendor_name[p]+'"/>'));
    console.log('<input type="text" name="cmpny_id" id="cmpny_id"'+p+' value="'+vendor_name[p]+'"/>');
});

Now i want to get values of text boxes i have tried following codes

$(this).attr("cmpny_id"+[2])

Help me guys

Upvotes: 0

Views: 2410

Answers (2)

JuanSedano
JuanSedano

Reputation: 1025

Im not sure what are trying to accomplish but you can try something like:

var id = 0;

$(document).on('click','#addVendor',function() {
      $("#vendor_name").append('<tr><td>'+id+': <input type="text" name="cmpny_id"></td></tr>');
      id = id+1;
});

$(document).on('click','#getVendor',function() {
  if (id == 0) {
      alert('At least add one vendor');
  } else {
    $("input[name=cmpny_id]").each(function(index) {
      alert('Index: '+index+' Value= '+$(this ).val());
    });
  }
});
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="vendor_name">
</table>
<button id='addVendor'>Add Vendor Name</button>
<button id='getVendor'>Get Vendor Name</button>

Upvotes: 1

jrummell
jrummell

Reputation: 43077

Try this sample. You had a the closing quote for the input id in the wrong place, and your selector to find the input later was also incorrect.

var p = 2;
var vendor_name = {};
vendor_name[p] = "some name";

// append a table cell with an input
$("#appendme").append($("<td>").html('<input type="text" name="cmpny_id" id="cmpny_id'+p+'" value="'+vendor_name[p]+'"/>'));

// display the value of cmpny_id2
alert($("#cmpny_id2").val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr id="appendme"></tr>
</table>

Upvotes: 0

Related Questions