Hi I just would like to get some information in this script. Script works fine and I uderstand this script only thing is why in this code txt=txt + coffee.value + " "; need to be txt variable twice? if I delete txt like this txt=coffee.value + " "; script still works fine. I just don't understand what does second txt. txt=txt + coffee.value + " "; Thank you for help <html> <head> <script type="text/javascript"> function createOrder() { coffee=document.forms[0].coffee; txt=""; for (i=0;i<coffee.length;++ i) { if (coffee.checked) { txt=txt + coffee.value + " "; } } document.getElementById("order").value="You ordered a coffee with " + txt; } </script> </head> <body> <p>How would you like your coffee?</p> <form> <input type="checkbox" name="coffee" value="cream">With cream<br /> <input type="checkbox" name="coffee" value="sugar">With sugar<br /> <br /> <input type="button" onclick="createOrder()" value="Send order"> <br /><br /> <input type="text" id="order" size="50"> </form> </body> </html>
earlier txt is cast as an empty string, overwriting previous 'orders' - which then keeps the concatenation of the value + " " as string (latter which is not really needed as txt is already a string and values from fields are usually string too - "sugar" and "cream" are 100% string). to be honest, this will work as just txt = coffee.value; (as you've discovered) - there's no need to typecast the variable by adding a string to it on either side