Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2006, 10:26 AM
Registered User
 
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default programmatically setting selections on a multiSELE

that using javascript and or ASP...any idea??

Rupen Anjaria.:)
------------------
We CAN'T avoid problems, but can solve it.
 
Old November 17th, 2006, 01:30 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Rupen!!
Both ways you can do the things :)
like
<script>


function setMultiSelect()
{
obj=document.getElementById("multiSel")
for(i=0;i<obj.length;i++)
{
 if(i%2==0)
  flag=false
else
  flag=true


  obj[i].selected=flag
}
}
</script>
<body onload="setMultiSelect()">
<select id="multiSel" name="multiSel" multiple size=10 >
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
<option >5</option>
<option >6</option>
<option >7</option>
</select>

<select id="multiSel1" name="multiSel1" multiple size=10 >
<option selected>1</option>
<option >2</option>
<option selected>3</option>
<option >4</option>
<option selected>5</option>
<option >6</option>
<option selected>7</option>

</select>
</body>

Hope this will help you

Cheers :)

vinod
 
Old November 17th, 2006, 06:43 AM
Registered User
 
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

Hi Vinod,

I haven't tried by think this should be like:

function setMultiSelect()
{
obj=document.getElementById("multiSel")

obj1 = document.getElementById("multiSel1")

for(i=0;i<obj1.length;i++)
{
 if(i%2==0)
  flag=false
else
  flag=true


  obj[i].selected=flag
}

Further, how exactly i%2 works?..
Please reply..


Rupen Anjaria.:)
------------------
We CAN'T avoid problems, but can solve it.
 
Old November 17th, 2006, 06:55 AM
Registered User
 
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

One more thing..what I am getting is a string format like 1,3,5,7..etc..so i cant set it like:

document.getele..("mselect") = request.form("mselect")



Rupen Anjaria.:)
------------------
We CAN'T avoid problems, but can solve it.
 
Old November 20th, 2006, 01:19 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Rupen!!
first select box named with id="multiSel" is shown how programitacally(javascript) you can set the multi select box.
on the other hand if you are using asp/php/jsp<serverside> you can use id="multiSel1" method where there you can find multiple selected values.

I have just shown an example in such a way that only alternative options of id="multiSel" should be selected so i used i%2==0 method.If your selected options is based on any other logic you can put here that logic,as you are getting string with comma separated then you can split that string and put the logic.As i expected these are values must be selected in ur multiselect selectbox.
so you need to put your selection logic on if(i%2==0) line.e.g
********
<script>


function setMultiSelect()
{
var str=""
strVal="1,3,5,7"
obj=document.getElementById("multiSel")
ArrStr=strVal.split(",")

    for(j=0;j<ArrStr.length;j++)
    {
    for(i=0;i<obj.length;i++)
        {
         if(parseInt(obj[i].value,10)==parseInt(ArrStr[j],10))
         obj[i].selected=true
        }
    }

}
</script>
<body onload="setMultiSelect()">
<select id="multiSel" name="multiSel" multiple size=10 >
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
</select>
</body>
*************
Hope this will help you

Cheers :)

vinod
 
Old November 20th, 2006, 07:59 AM
Registered User
 
Join Date: Apr 2005
Posts: 352
Thanks: 14
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rupen
Default

workng fine now....thanks..

Rupen Anjaria.:)
------------------
We CAN'T avoid problems, but can solve it.
 
Old November 20th, 2006, 10:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

Rupen,
    the % operator is also called Modulus and returns the remainder after the division (http://www.w3schools.com/js/js_operators.asp) this allows every other option to be selected in the example code.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Manage ComboBox Selections within JSP Ofirshani JSP Basics 1 March 23rd, 2007 08:01 AM
Two problems regarding selections freddy XSLT 10 November 3rd, 2006 07:52 PM
parameter: 6 selections maximum krema Reporting Services 0 February 20th, 2006 05:07 AM
Select Menu Multiple Selections phungleon HTML Code Clinic 2 July 20th, 2004 12:25 PM
setting mult. selections in listbox at page_load jtyson General .NET 1 April 14th, 2004 07:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.