Mike wrote:
I have a form that contains 240 "products". Each Product has a TR. Each TR
contains a Yes and No radio button and a Product-Qty text input.
A situation exists where I have to go through all the products and check the
No radio button for all the products. In javascript, that is easy enough.
When the No radio button is clicked, I have to put '0' in the Product-Qty
text input, and disable the Product-Qty text input. I create an array of all
the html id's of all the Product-Qty text inputs, and iterate through the
array, setting the .disabled = true
var sProdCodeQtys = new Array();
// fill the sProdCodeQtys array with id's of the text input's
Why not just generate the ID strings in the for loop below?
for (xxx= 0; xxx< sProdCodeQtys.length ; xxx++) {
Keep variables local unless there are good reasons to make them global.
It is also customary to use single digits for counters, usually 'i':
for (var i=0; i<sProdCodeQtys.length; i++) {
The use of a constant for sProdCodeQtys.length is recommended, though I
doubt you'll see a large speed improvement in this case.
var inputQty2 = document.getElementById(sProdCodeQtys[xxx]);
inputQty2.value = '0';
inputQty2.disabled = true;
}
This takes up to 30 seconds !! I'm dismayed it takes that long, anyone have
any better ideas ?
I suspect there is something seriously wrong elsewhere in your code.
The script below disables 240 text inputs in about 3 seconds using IE on
my ancient 500MHz PIII.
IE 5.2 on a similarly ancient Mac (400MHz G3) does it in about 1 second.
<title>Lists</title>
<script type="text/javascript">
function fixQty(el){
var name = 'qty-' + el.name.split('-')[1];
var textEl = el.form.elements[name];
if (el.checked && 'yes' == el.value){
textEl.disabled = false;
textEl.value = textEl.defaultValue;
return;
}
textEl.disabled = true;
textEl.value = 0;
}
function doAll(el){
var f = el.form;
var i=0;
var x;
while ( (x=f.elements['qty-' + i++]) ){
x.value = 0;
x.disabled = true;
}
}
</script>
<form action="">
<table>
<tr>
<td>
<input type="button" value="Disable all" onclick="doAll(this);">
<tr>
<td>
<input type="radio" name="rb-0" value="yes" checked
onclick="fixQty(this);">yes
<input type="radio" name="rb-0" value="no"
onclick="fixQty(this);">no<br>
<input type="text" name="qty-0" value="190">Quantity
<!-- Hack to generate HTML & not wast too much newsgroup bandwidth -->
<script type="text/javascript">
var theSource = [];
for (var i=1; i<240; i++){
theSource[theSource.length] = // Array.push not supported in IE 5.2
'<tr><td>'
+ '<input type="radio" name="rb-' + i + '" value="yes" checked '
+ 'onclick="fixQty(this);">yes'
+ '<input type="radio" name="rb-' + i + '" value="no"'
+ 'onclick="fixQty(this);">no<br>'
+ '<input type="text" name="qty-' + i + '" value="190">Quantity';
}
document.write( theSource.join('') );
</script>
</table>
</form>
--
Rob