Single variable multiple select box

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Sep 2008
Posts: 4
Reputation: wipeskim is an unknown quantity at this point 
Solved Threads: 0
wipeskim wipeskim is offline Offline
Newbie Poster

Single variable multiple select box

 
0
  #1
Sep 25th, 2008
Hi,

I created a multiple select box wich i would like to have one variable with all the results selected on the multiple select box such as variable_name = "result1|result2|result3"

This is what i have so far:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. oSelect = document.getElementById ('s1');
  2. var iNumSelected = 0;
  3.  
  4. for (var iCount=0; oSelect.options[iCount]; iCount++) {
  5. if (oSelect.options[iCount].selected == true) {
  6. iNumSelected ++;
  7. alert(oSelect[iCount].value); // this part alerts all the result within the loop
  8. }
  9. }

Now what i exactly need is to have a variable called "test" outside of the loop wich i have all the selected results separated with an "|", so that when i do alert(test) displays all results as a one result.

Hope someone can help

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Single variable multiple select box

 
0
  #2
Sep 25th, 2008
Declare a variable 'test' outside the loop and inside the loop append the value of the selected option along with a '|' to `test'. Try it. Did you get the desired result? If no, then why? What seems to be missing here? What do you think needs to be done to make it work the way you expect?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: wipeskim is an unknown quantity at this point 
Solved Threads: 0
wipeskim wipeskim is offline Offline
Newbie Poster

Re: Single variable multiple select box

 
0
  #3
Sep 26th, 2008
Hi, thanks for the reply but that's my problem on how to append everything on the variable , on php i usually do $test .= $array_var."|"; and outside the loop i just do echo $test, now how's that done in javascript?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: mohammed2raja is an unknown quantity at this point 
Solved Threads: 1
mohammed2raja mohammed2raja is offline Offline
Light Poster

Re: Single variable multiple select box

 
0
  #4
Sep 26th, 2008
  1. oSelect = document.getElementById ('s1');
  2. var iNumSelected = 0;
  3. var str="";
  4. for (var iCount=0; oSelect.options[iCount]; iCount++) {
  5. if (oSelect.options[iCount].selected == true) {
  6. iNumSelected ++;
  7. str+= "|" + oSelect.options[iCount].value
  8. }
  9. }
  10. alert(str);
Last edited by cscgal; Sep 26th, 2008 at 7:39 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: wipeskim is an unknown quantity at this point 
Solved Threads: 0
wipeskim wipeskim is offline Offline
Newbie Poster

Re: Single variable multiple select box

 
0
  #5
Sep 26th, 2008
Hi mohammed thanks for your answer, i tried that but it says object expetcted when i run the script. if i remove str+= to str= it works but it doesn't show all results.

Any alternatives?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: mohammed2raja is an unknown quantity at this point 
Solved Threads: 1
mohammed2raja mohammed2raja is offline Offline
Light Poster

Re: Single variable multiple select box

 
0
  #6
Sep 26th, 2008
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <script>
  5. function show()
  6. {
  7. var ss= document.getElementById('s1');
  8. var len= ss.options.length;
  9. var str="";
  10. var j=0;
  11. for(var i=0;i<len;i++){
  12. if(ss.options[i].selected == true)
  13. {
  14. if(j == 0)
  15. str= ss.options[i].innerHTML;
  16. else
  17. str += '|' + ss.options[i].innerHTML;
  18. j++;
  19. }
  20. }
  21. alert(str);
  22. }
  23. </script>
  24.  
  25. </HEAD>
  26.  
  27. <BODY>
  28. <select id="s1" size=3 multiple>
  29. <option> Raja1 </option>
  30. <option> Raja2 </option>
  31. <option> Raja3 </option>
  32. <option> Raja4 </option>
  33. <option> Raja5 </option>
  34. </select>
  35. <div>
  36. <input type="button" onclick="show();">
  37. </div>
  38. </BODY>
  39. </HTML>

I think this can help u.
Last edited by cscgal; Sep 26th, 2008 at 7:39 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: wipeskim is an unknown quantity at this point 
Solved Threads: 0
wipeskim wipeskim is offline Offline
Newbie Poster

Re: Single variable multiple select box

 
0
  #7
Sep 26th, 2008
thanks mohammed2raja it works perfectly many thanks!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC