virak Posted May 20, 2009 Share Posted May 20, 2009 Dear all,Could you please help with this code because i am very new with coding.I want to get the value & id from a checked box and insert into list box.but i try over 1house still can not done.Please help me. thanks before hand. <html><head><script type="text/javascript">function createOrder(){profile=document.forms[0].profile;for (i=0;i<profile.length;++ i) { if (profile[i].checked) { txt="<option>" + profile[i].value + "</option>"; } }document.getElementById("order").options= txt;}</script></head><body><p>How would you like your coffee?</p><form><input type="checkbox" name="profile" value="Virak">Name<br /><input type="checkbox" name="profile" value="24">Age<br /><br /><input type="button" onclick="createOrder()" value="Send order"><br /><br /><select id="order" size="5"></select></form></body> Thanks & regards,Virak Link to comment Share on other sites More sharing options...
dsonesuk Posted May 20, 2009 Share Posted May 20, 2009 Dear all,Could you please help with this code because i am very new with coding.I want to get the value & id from a checked box and insert into list box.but i try over 1house still can not done.Please help me. thanks before hand.<html><head><script type="text/javascript">function createOrder(){profile=document.forms[0].profile;for (i=0;i<profile.length;++ i){if (profile[i].checked){txt="<option>" + profile[i].value + "</option>"; }}document.getElementById("order").options= txt;}</script></head><body><p>How would you like your coffee?</p><form><input type="checkbox" name="profile" value="Virak">Name<br /><input type="checkbox" name="profile" value="24">Age<br /><br /><input type="button" onclick="createOrder()" value="Send order"><br /><br /><select id="order" size="5"></select></form></body> Thanks & regards,Virak Hi Virakto get this to work you need to use 'createElement', but also you need to clear options beforehand. see below:<html><head><script type="text/javascript">function createOrder(){ //clears current options from list var x=document.getElementById("order"); x.options.length=0; profile=document.forms[0].profile; for (i=0;i<profile.length;++ i) { if (profile.checked) { txt=profile.value; var newOption = document.createElement('OPTION'); newOption.text=txt; newOption.value=txt; x.options.add(newOption); } }}</script></head><body><p>How would you like your coffee?</p><form><input type="checkbox" name="profile" value="Virak">Name<br /><input type="checkbox" name="profile" value="24">Age<br /><br /><input type="button" onclick="createOrder()" value="Send order"><br /><br /><select id="order" size="5" style="width: 100px;"></select></form></body></html> Link to comment Share on other sites More sharing options...
virak Posted May 21, 2009 Author Share Posted May 21, 2009 Dear dsonesuk,Thanks you so much, it's working well :)Regards,Virak Link to comment Share on other sites More sharing options...
virak Posted May 22, 2009 Author Share Posted May 22, 2009 Dear dsonesuk,Sorry again dsonesuk, could you tell me one more thing from the above code,If i want to make newOption that adding to listbox done with auto selected, what should i do?I add "newOption.selected=txt;" it's error.i try with: if (profile[i].checked){ txt=profile[i].value; var newOption = document.createElement('OPTION');newOption.text=txt;newOption.value=txt;newOption.selected=txt;x.options.add(newOption);} Thanks & Regards,Virak Link to comment Share on other sites More sharing options...
justsomeguy Posted May 22, 2009 Share Posted May 22, 2009 The selected property is either true or false, not text. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 26, 2009 Share Posted May 26, 2009 Hi Virakto add the "newOption.selected=true;" is the correct way to make the item selected. BUT! with this added to the code as you added it, it will loop through each list item and mark this as 'selected', and when finished will highlight the last item in the list as the selected, even though all the items are also marked as selected. if you require the last item to be highlighted/selected do thisfunction createOrder(){//clears current options from listvar x=document.getElementById("order");x.options.length=0;profile=document.forms[0].profile;for (i=0;i<profile.length;++ i){ if (profile.checked){txt=profile.value; var newOption = document.createElement('OPTION');newOption.text=txt;newOption.value=txt;x.options.add(newOption);} }var y = document.getElementById("order").length;if(y){x[y-1].selected=true;}} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.