Jump to content

Get Value And Id From Checked Box Insert To Listbox.


virak

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...