submit selectionbox values

for a selectionbox widget, where the user selects items from one <select> box to another, unless the <option> tag has "selected" attribute, the items are not submitted to the server during a POST. what is the best technique for submitting these chosen item values? do people use javascript to apply "selected" to the <option> tags just before the form is submitted?
bhomassAsked:
Who is Participating?
 
matthew016Connect With a Mentor Commented:
You may nnot have the same id for more than one element !
You will have to do somthing like :

[...]

     function xyz() {
          var sel1 = document.getElementById('idSelectEelement1');
          var sel2 = document.getElementById('idSelectEelement2');
          for(i = 0; i < sel1.options.length; i++) sel1.options[i].selected = true;
          for(i = 0; i < sel2.options.length; i++) sel2.options[i].selected = true;
           return true;
     }

[...]

You can also make this code lighter by using a class atribute,
then you will have the same class attribute for all the elements.

Then you can use a library like jquery to get all the elemnts having that class attribute and modify their attribute in one line, for all the elements.

But the first solution will work.
0
 
matthew016Commented:
Yes, by jaavscript,

call a function on the submit which will select all the options

[...]

     function xyz() {
           var sel1 = document.getElementById('idSelectEelement');
      for(i = 0; i < sel1.options.length; i++) {
            sel1.options[i].selected = true;
      }
           return true;
     }

[...]

<form name=[...]     onsubmit="return xyz();">

[...]
0
 
najhCommented:
You'd only need to have something marked as selected if you wanted it to be a default attribute. It will send whatever else you've selected. You don't need a selected attribute.

Have I missed the point?
0
 
bhomassAuthor Commented:
mathew016's script works. but what I need is to call xyz for a number of <select> on the form, not just one.

I tried setting the same idattribute for multiple <select>, it seems to only pick up the first one.

how would I pass in something so that xyz will be applied to all of a set of <select> elements? can I use a wildcard in the idattribute, for example?
0
 
bhomassAuthor Commented:
matthew016, are you an expert in jquery? I am wondering how you can
"Then you can use a library like jquery to get all the elemnts having that class attribute and modify their attribute in one line, for all the elements."

I can get all the select's by $(".someClass"). but how would I implement
for(i = 0; i < sel1.options.length; i++) sel1.options[i].selected = true;
in one line?

another words, how can you get $(".someClass") to loop thru all its options in one line?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.