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

is it possible to get options from list without selection

100+
P: 106
i have a JS function which adds items to a list when the user clicks on a button ADD ITEM now i need to get all the items he added in the list without selecting them !! is that possible ?
i need to insert those items in the DB
any ideas ?
Nov 12 '08 #1
Share this Question
Share on Google+
7 Replies


gits
Expert Mod 5K+
P: 5,331
of course ... you just need to loop over the options that a select-node contains. could you post an example and tell what you want to retrieve the values or the 'description' of the items ... or both? how do you want to transfer the data?

kind regards
Nov 12 '08 #2

100+
P: 106
Well, this is my pretty simple JS function
Expand|Select|Wrap|Line Numbers
  1. function AddItem()
  2.     {   var opt = document.getElementById("List1");
  3.         var value=opt.options[opt.selectedIndex].text;
  4.         var newItem=document.createElement("option");
  5.         newItem.text=value;
  6.         document.getElementById("List2").options.add(newItem);
  7.   }
  8.  
and this is my List 2 (destination list) ==>
Expand|Select|Wrap|Line Numbers
  1. <select size="6" id="List2" name="list2[]" ></select>
these data are sent to DB.php which is supposed to take the posted data into the DB and thats the page at which i should loop through the options as u said but dont know how ??
Nov 12 '08 #3

gits
Expert Mod 5K+
P: 5,331
not exactly what i meant ... you need to loop at the client and add the options to the postdata onsubmit of your form or do you do an ajax-call?
Nov 12 '08 #4

100+
P: 106
i dont know if i get you But im doing this through onchange event for the select which submits the form
Expand|Select|Wrap|Line Numbers
  1.  select name="uniforms" onChange=this.form.submit();
and upon selection the user is presented with two list boxes with add to list and remove from list arrows ..so when he clicks on add to list(list2) and chooses all he wants from list1 to be added to list 2 he clicks on ADD button this button moves to the DB.php page which i was hoping to get the options in the list 2 WITHOUT selection ( just get all the items in there )
Nov 12 '08 #5

gits
Expert Mod 5K+
P: 5,331
so as i understand ... you have a second submit then? and this form with the two lists just need to take care of your problem? while adding and deleting you have two options. you could use the iterate method to get all options in the list or you may have a hidden field that contains them ... just update this while adding deleting items ... the other option could always do this on submit of the second form.

kind regards
Nov 12 '08 #6

100+
P: 106
Can u plz post some piece of code for any of the options u specified
Nov 12 '08 #7

gits
Expert Mod 5K+
P: 5,331
ok ... here is an example for how to get all option-values of a list 'foo' onclick of a button:

[HTML]<html>
<script type="text/javascript">
function get_options() {
var n = document.getElementById('foo');
var l = [];

for (var i = 0, o; o = n.childNodes[i]; i++) {
if (/^option$/i.test(o.tagName)) {
l.push(o.value);
}
}

alert(l);
}
</script>
<body>
<select id="foo">
<option value="foo1">foo1</option>
<option value="foo2">foo2</option>
<option value="foo3">foo3</option>
<option value="foo4">foo4</option>
<option value="foo5">foo5</option>
</select>
<input type="button" onclick="get_options()" value="get options"/>
</body>
</html>
[/HTML]
now you just need to do anything you want with those values.

kind regards
Nov 12 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.