-1

i have created form which have text box in foreach loop for multiple textbox, textbox have dynamic Id. so id for textbox creating like id="cmbeditqty-1". but if two textbox will create then how can i get those two values in different variable?

<td>
    <input class="form-control" type="text" class="cmbeditqty" id="cmbeditqty-<?php echo $i; ?>" style="width: 250px;"  name="qty"> 
</td>  
<td>
    <input class="form-control" type="text" id="cmbeditprice-<?php echo $i; ?>" style="width: 250px;"  name="itemprice"> 
</td>  
David
  • 208,112
  • 36
  • 198
  • 279

1 Answers1

1

From JavaScript's perspective they're not "dynamic", they've id values like any other. You would just need to select them based on some common way to identify them. In this case they all start with the same string, so you can use that in a selector:

$('input[id^="cmbeditqty-"]')

Like any other jQuery selector, this will return a list of matching elements. You can pull the values from elements in that list like any other.

David
  • 208,112
  • 36
  • 198
  • 279
  • getting TypeError: $ is not a function var check = $('input[id^=cmbeditqty-]'); – Rahul Patil Sep 28 '17 at 11:27
  • @RahulPatil: Sounds like you forgot to include jQuery: https://stackoverflow.com/questions/2194992/jquery-is-not-defined – David Sep 28 '17 at 11:28
  • no sir i got an alert(123); which i put for testing – Rahul Patil Sep 28 '17 at 11:30
  • @RahulPatil: Your last comment doesn't make sense. Please elaborate. – David Sep 28 '17 at 11:31
  • $(document).ready(function ($) { var mainqty = $("#cmbqty").val(); $('.updateSingleSku').click(function($){ alert(123); var check = $('input[id^=cmbeditqty-]').val(); alert(check); }); }); – Rahul Patil Sep 28 '17 at 11:32
  • @RahulPatil: Your event handlers are overriding the `$` variable. `$` is the default jQuery function. You are overwriting it with something that *isn't* a function. Hence the error. Don't overwrite the `$` variable in your event handlers. – David Sep 28 '17 at 11:34
  • Remove the `$` from `function($)` – GreyRoofPigeon Sep 28 '17 at 11:34
  • yes, but its showing only first textbox value.. how can i get bot text box value – Rahul Patil Sep 28 '17 at 11:40
  • @RahulPatil: The code in your comment is only attempting to display one value. If you want all of the values then you need to *loop* over the results. For reference: https://stackoverflow.com/questions/9057388/getting-multiple-elements-value-with-jquery – David Sep 28 '17 at 11:41
  • by using each() function. i got both values in one variable. i want those values for comapire third variable which is outside o each loop. – Rahul Patil Sep 28 '17 at 12:51
  • @RahulPatil: This sounds like a separate problem. Originally you'd asked how to select multiple elements which start with the same `id` string, which has been answered. If you've structured your code in such a way that some other problems also exist, I suggest you open a Stack Overflow question in which you demonstrate that problem and ask about it. – David Sep 28 '17 at 12:54