943,884 Members | Top Members by Rank

Ad:
Sep 25th, 2008
0

Single variable multiple select box

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wipeskim is offline Offline
4 posts
since Sep 2008
Sep 25th, 2008
0

Re: Single variable multiple select box

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?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 26th, 2008
0

Re: Single variable multiple select box

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wipeskim is offline Offline
4 posts
since Sep 2008
Sep 26th, 2008
0

Re: Single variable multiple select box

javascript Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Light Poster
mohammed2raja is offline Offline
30 posts
since Sep 2008
Sep 26th, 2008
0

Re: Single variable multiple select box

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wipeskim is offline Offline
4 posts
since Sep 2008
Sep 26th, 2008
0

Re: Single variable multiple select box

javascript Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Light Poster
mohammed2raja is offline Offline
30 posts
since Sep 2008
Sep 26th, 2008
0

Re: Single variable multiple select box

thanks mohammed2raja it works perfectly many thanks!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wipeskim is offline Offline
4 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: image as link
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Drop Down Menu with Sucker Fish





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC