Re: Passing HTML array index to JS?
| From: | Philip Thompson | Date: | Mon, 07 Dec 2009 23:56:44 +0000 |
| Subject: | Re: Passing HTML array index to JS? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-300299@lists.php.net to get a copy of this message | ||
On Dec 7, 2009, at 5:02 PM, Skip Evans wrote:
> Hey all,
>
> I have an HTML field like this
>
> <input type="text" name="qty[]" value="!!quantity!!"
> size="4" style="text-align: right;" onblur="calculateBidUnit();">
>
> ... and what I need to do is pass to the calculateBidUnit function the value of quantity, do a
> calculation on it and plug into this field.
>
> <input type="text" name="bid_unit_value[]" value=""
> size="4">
>
> Which of course I know how to do for non-array values, but not sure how to get the values to do
> the calculation on the JS side if the fields are in an array.
>
> Any help, as always, is greatly appreciated,
> Skip
This question is probably more appropriate for a JS list... but I'm not mean, so I'll let
you know. The easiest way would be to update the blur event qty input:
onblur="calculateBidUnit(this.value);"
Now you have the value sent to that function. Another way would be to give your input and output an
id each - let's say 'qty' and 'bid_unit_value', respectively. Then
reference those ids in your function. This gives you the following:
<input type="text" name="qty[]" value="!!quantity!!"
onblur="calculateBidUnit();" id="qty" />
<input type="text" name="bid_unit_value[]" value=""
id="bid_unit_value" />
<script type="text/javascript">
function calculateBidUnit () {
var qty = document.getElementById('qty');
var buv = document.getElementById('bid_unit_value');
buv.value = qty.value;
}
</script>
I'll give you a small warning. All browsers are not alike, so my recommendation would be to use
a library that handles the cross-browser compatibility portion of javascript (I use Mootools).
Nonetheless, the above *should* work. Learning the bare bones of javascript will help with the more
complicated stuff and you'll be smarter for it! =P
Hope that helps,
~Philip