php - value won't display by each row -


i have problem.
array return me dynamically data it.

<?php  foreach($itemarray $key => $value) {     echo '     <tr><td height="30" valign="middle">'.$value['nr'].'</td>     <td valign="middle">'.$value['product'].'</td>     <td valign="middle">'.$value['describe'].'</td>     <td valign="middle" id="price_'.$key.'">'.$value['price'].'</td>     <td valign="middle" class="box_darker" id="amount_'.$key.'">     <input name="field_amount_'.$key.'" id="field_amount_'.$key.'" class="field_amount" type="text" /></td>     <td valign="middle" id="price_'.$key.'">$key</td></tr>'; } ;?>   

now test if value returns me right 1 when click on belong field (td/price)

$(document).on('click', '[id^=price_]', function(){ var amount = $('[id^=field_amount_]').val(); alert(amount); });   

but nevermind wich field (td/price) in each row click, alert me value first row! maybe because data loads dynamically?

sorry poor english.

that's because perform second search on global scope well, , val() return value first element. use $(this).parent().find() instead:

$(document).on('click', '[id^=price_]', function(){     var amount = $(this).parent().find('[id^=field_amount_]').val();     alert(amount); });   

alternately, should able use .closest():

$(document).on('click', '[id^=price_]', function(){     var amount = $(this).closest('[id^=field_amount_]').val();     alert(amount); });  

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -