V Jay V Jay - 9 days ago 6
Javascript Question

how to get textbox value in jquery function..?

I am trying to get for loop different textbox value in jquery function.

MY php code :

<?php
$qstid = $addnew334->id;
$dd = DB::table('tbltplatematerial')->where('qteid',$qstid)->get();
foreach($dd as $d1)
{

$ing_id = $d1->id;
?>

<tr>
<td><?php echo $d1->name;?></td>
<td><input type="text" name="txtingprice" id="<?php echo $ing_id;?>" value="" ></td>
<td><input type="checkbox" value="<?php echo $d1->id; ?>" onClick="addpriceing(<?php echo $ing_id; ?>,<?php echo $qstid;?>,<?php echo $value;?>,<?php echo $value11;?>)" id="checkid"></td>

<?php
}
?>


My jquery function:

function addpriceing(ing_id,qstid,user_product_id,temp_id)
{
var price = $("input[name='txtingprice']").val();
alert(price);
$.ajax({
url: '<?= URL:: to('addpriceing1') ?>',
type: 'GET',
//dataType: "json",
async : false,
data:{
'price':price,
'ing_id':ing_id,
'qstid':qstid,
'user_product_id':user_product_id,
'temp_id':temp_id,

},
success: function(e)
{
if(e == 0)
{
console.log("Successs");
alert("success");
}
else
{
alert('error');
}
}
});

}


I am trying to get all txtingprice value in jquery function. Please help.

Answer

Since you are creating multiple textbox using foreach so instead of name="txtingprice" use name="txtingprice[]". Also you need to loop on field name to get the value as:

var price = $('input[name="txtingprice[]"]').map(function(){
       return this.value
   }).get()
   alert(price);

Working demo