2

How do I get multiple textbox values that are dynamically created in jquery

Sample code:

<?php
$i=1;
while($i<10)
{
     echo "<input type='textbox' class='qty' id='qty_$id'>";
     echo "<input type='textbox' class='item' id='item_$id'>";
     $i++;
}
echo '<input class="btn_transaction" id="btn_update" type="button" style="width:auto" value="Update">';


?>

jquery

<script>
jQuery(document).ready(function(e){
  $("#btn_update").click(function() {
        $.each($('.qty'), function() {
        var qty =  $(this).val();
        alert(qty);  // im getting qty here. In the same way i want item also, how to get item value here
        jQuery.post('../ajax/ajax_cart.php',{section:'update_tocart',qty:qty},function(data){

        });             
     });

  });

});            
</script>

thanks in advance :)

0

6 Answers 6

3

DEMO

Try this

$("#btn_update").click(function() {
        $(".qty").each(function() {
           var qty =  $(this).val();
           alert(qty);      
     });

  });

Hope this helps,Thank you

Sign up to request clarification or add additional context in comments.

1 Comment

its really good for scenario like mine where fields are generated dynamically as per user requirement!!!
0
jQuery(document).ready(function(e){
$("#btn_update").click(function() {
    var data = {};
    $( "input" ).each(function() {
        data[$(this).attr('id')] = $(this).val();
        jQuery.post('../ajax/ajax_cart.php',{section:'update_tocart',data:data},function(data){

        });             
    });
});

});

Try above!! You can find all input elements! You can then store it in a json object and pass to ajax call.

7 Comments

im not getting ur code. im new to jquery and json. can u please explain in breif. i want only those 2 text box values which are in while loop
The above code will check all input fields in the DOM and then set in the data object. So the final data object will something like {qty:'value', item:'value'}, so now data = {qty:'value', item:'value'}. So now you can send the data object in ajax call and you collect it in ajax_cart.php page through $_POST['data'] as I am passing it like jQuery.post('../ajax/ajax_cart.php'{section:'update_tocart',data:data},function(data){});. If you have some other input field in your page which you don't want to post then in place of $( "input" ) you can filter your selector. Make sense!
how can i retrive values in ajax page.. can u give me sample.. i have to use foreach loop ryt?
Yes, you have to do loop because may be your $_POST['data'] is more then 2 ans you are not really sure about that. So you can do a loop and then you can do whatever operation you want! Does that make sense?
data[$(this).attr('id')] = $(this).val(); what is 'id' refers to?
|
0

Try this

$.each($('.qty'), function() {
    var qty =  $(this).val();
    var item = $(this).siblings('input.item').val();
});

1 Comment

no not working... same error when i give alert(item) ---- undefined
0

Try with .next() like

var item = $(this).next('.item').val();

So it would be

$('.qty').each(function() { // Or     $.each('.qty' , function() {
    var qty =  $(this).val();
    var item = $(this).next('.item').val();
 });

See the FIDDLE And FIDDLE2

2 Comments

item is not coming. when i give alert --- item is not defined. i have used ur above code
no it is not working. it showing only last enterd values. i want all the values
0

try

$('.qty').each(function() { 

var qty =  $(this).val();
var item =  $(this).closest('.item').attr('value'); })

Comments

0

add class to all your textbox e.g textbox

<script type="text/javascript">
    var Work = [];
var textboxes = document.getElementsByClassName('name_list');
var boxlengt = document.getElementsByClassName('name_list').length;
for(var i=0;i<boxlengt;i++){
    Work.push(textboxes[i].value)
}
Work = Work.toString();
</script>

All the values are being stored in the items array. Now its up to you how you want each values to be used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.