views:

91

answers:

3

I have a multiple select. How can make, that all options will be selected always? Or how make a selecting all options ob submit. I have only class of select(not ID).

+1  A: 

I'm not 100% sure that this is what you're asking for, but here it goes:

<SELECT MULTIPLE="yes" ID="multipleSelect">
<OPTION VALUE="1" SELECTED="selected">Option 1</OPTION>
<OPTION VALUE="2" SELECTED="selected">Option 2</OPTION>
<OPTION VALUE="3" SELECTED="selected">Option 3</OPTION>
<OPTION VALUE="4" SELECTED="selected">Option 4</OPTION>
<OPTION VALUE="5" SELECTED="selected">Option 5</OPTION>
<OPTION VALUE="6" SELECTED="selected">Option 6</OPTION>
</SELECT>

<INPUT TYPE="submit" ID="submit" value="Go!"/>

For the dynamic option (with jQuery):

$("#submit").click(function() {  
  $("#multipleSelect option").each(function() {
    $(this).attr("selected", "selected");
  });
});

Untested.

Good luck.

kitsched
I need to make it dinamically.
Alexander.Plutov
You should've been more specific in your question.
kitsched
Added dynamic option.
kitsched
A: 

Hi, Here's a slight modific to Kitsched's code to ensure that you're able to select the button/select control if you only have the class and not the id.

<html id="html">
<body id="body">
<script src="file:\\\D:\Sidharth\javascript\jquery-1.4.2.min.js"></script>

<script type="text/javascript">
    $(document).ready(function () {

        function SelectAll() {
            $(".someClass").children().each(function () { $(this).attr("selected", "selected"); });
        }

        $(".clickme").bind("click", SelectAll);


    });
</script>
<input type="submit" class="clickme" />
<select multiple="yes" class="someClass">
<OPTION VALUE="1" id="one">Option 1</OPTION> 
<OPTION VALUE="2" id="two">Option 2</OPTION> 
<OPTION VALUE="3" id="three">Option 3</OPTION> 
<OPTION VALUE="4" id="four">Option 4</OPTION> 
</select>

</div>
</body>
</html>

If you've IDs for either button or select just replace the . with # in script. For e.g. if button's id is clickme instead of class just use #clickme instead of .clickme.

Hope this is what you're looking for.

Sidharth Panwar
+3  A: 

I think you can even write your replacement line like this:

$(".someClass option").attr("selected", "selected");

to avoid the .each() loop.

Ben