By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Looping and Removing all options

P: n/a
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

Aug 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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

Danny

Aug 26 '06 #2

P: n/a
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #3

P: n/a

Danny wrote:
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.

Aug 26 '06 #4

P: n/a

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


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.
--
Rob

Aug 26 '06 #5

P: n/a
oz****@iinet.net.au wrote:
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.
--
Rob
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+delCo st2
document.getElementById("priceProducts").value=Hol den_Total+Ford_Total;
// changed
document.getElementById("priceShipping").value=del Cost1+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

Aug 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.