3

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 ?

3
  • $(this).parents("tr").find('input[name="arrondi_devis_ht"]').val(new_prix_ht); etc... Commented Jun 25, 2012 at 15:18
  • Did you think to google something like "how to set input value with jquery"? Commented Jun 25, 2012 at 15:27
  • @nnnnnn : I tried googling, searched on this site and the jquery doc, but maybe my terms of research were too specific, and it didn't return any pertinent anwser. I apologize and will try better next time before asking. Commented Jun 25, 2012 at 15:40

3 Answers 3

7

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);
Sign up to request clarification or add additional context in comments.

Comments

1
$(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.

Comments

1
$(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);

5 Comments

It's déjà vu all over again... (OK, this is slightly different...)
@nnnnnn. What do you mean? is traversing up until document vs just until the <tr> is only "slightly different"?
At first glance there were three near-identical answers. (But then I noticed you'd used .closest(), which is definitely better though it kind of spoils the Déjà vu joke.)
@nnnnnn. I'm sorry I didn't mean to spoil your joke... :). Actually I could write a better answer if I knew what is this and why he needs to go up the <tr>
$(this) is one of the text inputs. Basically, each one of their individual values depend on the values of the other two, so every time i change a value of one of the text inputs (the event triggering this is key up), the other two have to be changed. Since they're on the same line, i go up to the tr and then come down to find the other two inputs and change their values. I'm still a beginner, so i'm all ears if you have a more optimum solution :) Thanks already for your contribution.

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.