Looping and Removing all options

G

Gregc.

Hi

Is this correct to clear select items?

function(sel){
var selected = sel.options[w].text;
var name=sel.name;

(i=1; i<sel.length=0,i++){
sel.options.length=0;}

if(var name="test"){
for (i=0;i< test;i++)
..
..
..}



<select name="test" id="test" onchange="updateOrder(this);">

<option>0</option>
<option>1</option>


<td bgcolor="#FFE9BB" class="borderBR"><select name="example" size="4"
multiple="multiple" id="example"> <option>0</option>

Greg
 
E

Evertjan.

Danny wrote on 26 aug 2006 in comp.lang.javascript:
Just sel.length=0 will do all :) .

A fine answer.

A pity, Danny,
many of us do not know
what you are talking about,
if you do not quote on usenet.
 
G

Gregc.

Danny said:
Just sel.length=0 will do all :) .


How about when you have:
function(test){

}


<form>
<select name=s1 multiple=true size=5 onchange(this)>
<option>0
<option>1
<option>2
<option>3
<option>4
<option>5
</select>


and your writing to:
<select name=s2 multiple=true size=5 onchange(this)>
<option> </option>

and every time you make an option it writes to s2, and if the user
changes their mind instead of writing the new option under the old one,
you write on top of the old one.
 
O

ozfred

Gregc. said:
How about when you have:
function(test){

}

A function that does nothing, and isn't called at all?

<form>
<select name=s1 multiple=true size=5 onchange(this)>

Did you mean:

<select ... onchange="test(this)">

Using onchange with select elements is known to produce unwelcome
sideeffects, I don't think it's the right solution here.

<option>0
<option>1
<option>2
<option>3
<option>4
<option>5
</select>


and your writing to:
<select name=s2 multiple=true size=5 onchange(this)>
<option> </option>

and every time you make an option it writes to s2, and if the user
changes their mind instead of writing the new option under the old one,
you write on top of the old one.

If you post working code, or at least something that demonstrates what
you are trying to do, you might get better responses. If you can't
write the code, write a clear description of what you are trying to do.
 
G

Gregc.

If you post working code, or at least something that demonstrates what
you are trying to do, you might get better responses. If you can't
write the code, write a clear description of what you are trying to do.

Hi Rob

Attached is the code that I am currently trying to get to work.

<html>

<head>

<script type="text/javascript">



function updateOrder(obj) {

var i;
var vehicleType=obj.name;
var w=obj.selectedIndex;
var selected_text = obj.options[w].text;
var car1=document.getElementById("carType");
var qty1=document.getElementById("orderQty");
var qty2=parseInt(obj.options[w].text);
var orPrice=document.getElementById("orderPrice");
var holdern_Price = 10;
var ford_Price = 20;
var del=50;
var del_c=10;
var delCost1=0;
var delCost2=0;
var totalDel=0;
var final_total=0;
var Holden_Total=0;
var Ford_Total=0;

//Clears out all options
for (i=0 ;i<document.form1[0].len.length ;i++)
{
obj.length=0
}

//Holden

if(vehicleType=="Holden"){

for (i=0;i< vehicleType;i++)
car1.options[car1.options.length]=new Option("Holden");
qty1.options[qty1.options.length]=new Option(selected_text);
orPrice.options[orPrice.options.length]=new Option(holdern_Price);
Holden_Total = holdern_Price*qty2;
var delCost1=del+(del_c*qty2);

}

//Ford

else if(vehicleType=="Ford"){
for(i=0;i< vehicleType;i++)
car1.options[car1.options.length]=new Option("Ford");
qty1.options[qty1.options.length]=new Option(selected_text);
orPrice.options[orPrice.options.length]=new Option(ford_Price);
Ford_Total= ford_Price*qty2;
var delCost2=del+(del_c*qty2)

}

// total figure
final_total=Holden_Total+Ford_Total+delCost1+delCost2
document.getElementById("priceProducts").value=Holden_Total+Ford_Total;
// changed
document.getElementById("priceShipping").value=delCost1+delCost2;
document.getElementById("priceTotal").value=final_total;
}



</script>
</head>
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0"
style="text-align:center">
<form name="form1" method="post" action="" onsubmit="">
<select name="Holden" id="Holden" onchange="updateOrder(this);">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<select name="Ford" id="Ford" onchange="updateOrder(this);">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</form>
<td bgcolor="#FFE9BB" class="borderBR"><select name="Type of Car"
size="10" id="carType">
<option>Car Ordered</option>
</select> </td>
<td bgcolor="#FFE9BB" class="borderBR"><select name="orderQty" size="4"
multiple="multiple" id="orderQty">
<option>0</option>
<td bgcolor="#FFE9BB" class="borderB"><select name="orderPrice"
size="4" multiple="multiple" id="orderPrice">
<option>$0.00</option>

</select></td>
</tr>
<td bgcolor="#FFE9BB">&nbsp;</td>
<td align="right" bgcolor="#DFAF6F"
class="borderBR"><strong>Product Total: </strong></td>
<td bgcolor="#FFE9BB" class="borderB"><input
name="priceProducts" type="text" id="priceProducts" size="8" /></td>
</tr>
<td align="right" bgcolor="#DFAF6F" class="borderBR"><strong>Shipping
Costs: </strong></td>
<td bgcolor="#FFE9BB" class="borderB"><input
name="priceShipping" type="text" id="priceShipping" size="8" /></td>
</tr>
<td bgcolor="#FFE9BB">&nbsp;</td>
<td align="right" bgcolor="#DFAF6F"
class="borderBR"><strong>Order Total: </strong></td>
<td bgcolor="#FFE9BB" class="borderB"><input
name="priceTotal" type="text" id="priceTotal" size="8" /></td>
</tr>


</body>

</html>

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top