Jump to content

getElementsByName wildcard


Muiter

Recommended Posts

I aheb this code:

function calculatie(selectVeld, nr){document.getElementsByName('materiaalkost[]')[nr].value  = ((document.getElementsByName('gewicht_stk[]')[nr].value * (document.getElementsByName('inkoop_std')[0].value / 1000)) * ((document.getElementsByName('afval')[0].value / 100) + (1 * 1))).toFixed( 2 );}onChange="calculatie(this, <?php echo 0 ?>)"

All elements are in an for loop. Except for document.getElementsByName('inkoop_std').value, this element is not reconized. How can I use an element in the above code that is is not in the for loop?

Link to comment
Share on other sites

I'm not sure what you mean by for loop? Also, if you are only echoing 0, why have it with PHP at all? Perhaps you should show us all the code so we can get a better idea of what you are trying to do?getElementsByName will return all elements with a name. Instead, since you are using different names for everything, why not just give an id to the elements you need to read the values from and use document.getElementById?

Link to comment
Share on other sites

The only reason I can think getElementsByName('inkoop_std')[0] is not recognized is that it does not exist. Have you checked the spelling? Are you sure that is the value of a name attribute, not an id attribute? Is there something malformed about the HTML that might keep the element from existing in the DOM?Oh yeah. Is that name actually modeled like the others: inkoop_std[] ?

Link to comment
Share on other sites

This is the field that is outside the loop:

<input type="text" name="inkoop_std" size="2" value="<?php if($inkoop_std != ''){ echo $inkoop_std; } ?>" onChange="calculatie(this, <?php echo 0 ?>)" />

This should manipulate the field in this loop:

<input type="text" name="inkoop[]" value="<?php echo $inkoop[$i]; ?>" size="5" onChange="calculatie(this, <?php echo $i ?>)" /><input type="text" name="materiaalkost[]" value="<?php echo $materiaalkost[$i]; ?>" size="7" style="background-color: #e7e7e9" readonly="readonly" />

Link to comment
Share on other sites

Well, that looks correct. Try looking at the element in your DOM inspector. See if the values are correct.And just so we're all clear on what's happening, how do you know that document.getElementsByName('inkoop_std')[0] is not recognized? What error comes up in your console?

Link to comment
Share on other sites

I think I have made an error with

onChange="calculatie(this, <?php echo 0 ?>)"

This part is not in the for loop and now only the field with number 0 is effected.

value="<?php echo $materiaalkost[0]; ?>"

The other numbers are not effected.

value="<?php echo $materiaalkost[1]; ?>"

value="<?php echo $materiaalkost[2]; ?>"

value="<?php echo $materiaalkost[3]; ?>"

value="<?php echo $materiaalkost[4]; ?>"

Link to comment
Share on other sites

If you're printing a zero only, it should be obvious that it's only going to work with record 0. If you print only a 1, it would only work with record 1. You need to print each thing you want to work with, not just one thing you always print. If you always printed a 0 you wouldn't need PHP at all, you would just write a 0 there. It sounds like you need to print those fields in a loop also, where each field changes a specific one of the other fields. If you want one field that may change any of the others, then you'll need some other way to pick which field it changes, like a radio button or select list.

Link to comment
Share on other sites

This is the part of the loop, I have removed alle fields that are not in the JS code.

<?php for ($i=0; $i <= $aantal_regels_corr; $i++){	?><tr>	<td class="gewicht">		<input type="text" name="gewicht_stk[]" value="<?php echo $gewicht_stk[$i]; ?>" size="7" style="background-color: #e7e7e9" readonly="readonly" />	</td>	<td class="inkoop">		<input type="text" name="inkoop[]" value="<?php echo $inkoop[$i]; ?>" size="5" onChange="calculatie(this, <?php echo $i ?>)" />	</td>	<td class="mat_kost">		<input type="text" name="materiaalkost[]" value="<?php echo $materiaalkost[$i]; ?>" size="7" style="background-color: #e7e7e9" readonly="readonly" />	</td></tr><?php } ?>

I need to be able to calculate with this general fields. When one of this fields are changed alle the fields in te loop should change.

<td><?php echo $lang['calculatie_inkoop']; ?>:</td><td><input type="text" name="inkoop_std" size="2" value="<?php if($inkoop_std != ''){ echo $inkoop_std; } ?>" onChange="calculatie(this, <?php echo 0 ?>)" /> per ton</td><td><?php echo $lang['calculatie_afval']; ?>:</td><td><input type="text" name="afval" size="2" value="<?php if($afval != ''){ echo $afval; } else { echo '10'; } ?>" onChange="calculatie(this, <?php echo 0 ?>)" /> %</td>

the problems seems te be onChange="calculatie(this, <?php echo 0 ?>)" in the general fields, because I have used 0 only that part of the loop is changed. When I use calculatie(this) or calculatie() it's also not working.

Link to comment
Share on other sites

Ok, I have added an form field inside the loop:

<input type="hidden" name="inkoop_std_copy[]" value="<?php echo $inkoop_std_copy[$i]; ?>" size="5" onChange="calculatie(this, <?php echo $i ?>)" style="background-color: #e7e7e9" readonly="readonly" />

The changed java script:

function calculatie(selectVeld, nr){// Stuur informatie terug// Kopieer materiaalkostenvar i, totaal = 0;var elems = document.getElementsByName('inkoop_std_copy[]');var l = elems.length; for(i=0; i<l; i++){	elems[i].value = document.getElementById('inkoop_std').value;}// Materiaalkostenif(document.getElementsByName('inkoop[]')[nr].value == ''){	document.getElementsByName('materiaalkost[]')[nr].value  = ((document.getElementsByName('gewicht_stk[]')[nr].value * (document.getElementsByName('inkoop_std_copy[]')[nr].value / 1000)) * ((document.getElementsByName('afval_perc[]')[nr].value / 100) + (1 * 1))).toFixed( 2 );}else{	document.getElementsByName('materiaalkost[]')[nr].value  = ((document.getElementsByName('gewicht_stk[]')[nr].value * (document.getElementsByName('inkoop[]')[nr].value / 1000)) * ((document.getElementsByName('afval_perc[]')[nr].value / 100) + (1 * 1))).toFixed( 2 );}}

The field is copied into all formfield correctly but when I change inkoop_std only the first (= 0) part of the loop is changed.

Link to comment
Share on other sites

I'm confused when you say "only the first part of the loop is changed". The loop is a control structure, it's not a field. What specific fields are you trying to change? When you enter a value into inkoop_std, what, specifically, do you want to happen, in terms of field names? Are you trying to update all inkoop fields? All materiaalkost fields? All inkoop_std_copy fields?

Link to comment
Share on other sites

If inkoop_std is changed the value is correctly copied to all inkoop_std_copy fields in the loopFor some reason all other javascript calculation is not executed altough inkoop_std_copy is changed (= copied from inkoop_std_copy). I have set onChange="calculatie(this, <?php echo $i ?>)" in inkoop_std_copy.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...