Streltsov
Streltsov

Reputation: 417

How to set the value of text inputs in a table in jQuery?

I have these text inputs in a table :

<td><input type='text' name='arrondi_devis_ht' class='calcul_total' size='8'></td>
<td><input type='text' name='arrondi_devis_ttc' class='calcul_total' size='8'></td>
<td><input type="text" name='marge_pourcent' class='calcul_total' size='5' value='20,0'></td>

I successfully get their value by performing this :

var prix_unitaire_ht=parseFloat($(this).parents("tr").find('input[name="arrondi_devis_ht"]').val().replace(',','.'));
var prix_unitaire_ttc=parseFloat($(this).parents("tr").find('input[name="arrondi_devis_ttc"]').val().replace(',','.'));
var marge_pourcent=parseFloat($(this).parents("tr").find('input[name="marge_pourcent"]').val().replace(',','.'));

So i figured i could use a similar mechanism to set their values and tried this :

$(this).parents("tr").find('input[name="arrondi_devis_ht"]').value=new_prix_ht;
$(this).parents("tr").find('input[name="arrondi_devis_ttc"]').value=new_prix_ttc;
$(this).parents("tr").find('input[name="marge_pourcent"]').value=new_marge;

Yet it does not work and their value remain unchanged. I've tried with .val instead of value, also to assign them an id and to do find("#id").value instead of the above without any result.

Could anyone please tell me what I'm doing wrong ?

Upvotes: 2

Views: 6710

Answers (3)

gdoron
gdoron

Reputation: 150253

$(this).closest("tr").find('input[name="arrondi_devis_ht"]').val(new_prix_ht);
$(this).closest("tr").find('input[name="arrondi_devis_ttc"]').val(new_prix_ttc);
$(this).closest("tr").find('input[name="marge_pourcent"]').val(new_marge);

You can cache the <tr> than, you can simplify your code to:

var row = $(this).closest('tr');
row.find('input[name="arrondi_devis_ht"]').val(new_prix_ht);
row.find('input[name="arrondi_devis_ttc"]').val(new_prix_ttc);
row.find('input[name="marge_pourcent"]').val(new_marge);

Upvotes: 1

Josh Mein
Josh Mein

Reputation: 28635

You need to use that val() method to set the value of an input. This is what you are looking for:

$(this).parents("tr").find('input[name="arrondi_devis_ht"]').val(new_prix_ht);
$(this).parents("tr").find('input[name="arrondi_devis_ttc"]').val(new_prix_ttc);
$(this).parents("tr").find('input[name="marge_pourcent"]').val(new_marge);

Since they are all in the same row, you may want to reuse the initial selector like so:

var $row = $(this).parents("tr");

$row.find('input[name="arrondi_devis_ht"]').val(new_prix_ht);
$row.find('input[name="arrondi_devis_ttc"]').val(new_prix_ttc);
$row.find('input[name="marge_pourcent"]').val(new_marge);

Upvotes: 6

thecodeparadox
thecodeparadox

Reputation: 87073

$(this).parents("tr").find('input[name="arrondi_devis_ht"]').val(new_prix_ht);
$(this).parents("tr").find('input[name="arrondi_devis_ttc"]').val(new_prix_ttc);
$(this).parents("tr").find('input[name="marge_pourcent"]').val(new_marge);

You need to use .val() to set value to a input field.

Upvotes: 1

Related Questions