Connecting Tech Pros Worldwide Forums | Help | Site Map

[Javascript] Hidden fields

Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#1: Sep 1 '08
[Javascript] Hidden field

Hello my friends.

This is a htm page that contains a form open in window popUp ( page daughter ).

This form insert a value in the one hidden field called "tec", in other form open in the page mother.

As you can see the select contains multiple attribute.

The problem is that by selecting more names from select tec form page daughter, in the hidden field called "tec" form page mother I have only the first name, for example:

I select value gabriele and giuseppe form page daughter, in the hidden field called "tec" form page mother I have only gabriele.

Why?
Can you help me ???



Expand|Select|Wrap|Line Numbers
  1. <script language="Javascript">
  2.  
  3. <!--
  4.  
  5. function insertf1(f1) {
  6.  
  7.    if ( FORM1.tec.value.length > 0 && FORM1.opr.value.length > 0 ) {
  8.         window.opener.document.FORM1.tec.value=f1;
  9.         alert("Dati correttamente salvati.");
  10.         window.close();
  11.  
  12.     } else {
  13.  
  14.         alert("Dati obbligatori."); 
  15.         FORM1.tec.focus(); 
  16.  
  17.    }
  18.  
  19. }
  20.  
  21. // -->
  22. </script>
  23. </head>
  24.  
  25. <body>
  26.  
  27. <form name="FORM1">
  28.  
  29. <select size="15" name="tec" multiple>
  30.  
  31. <option>Seleziona</option>
  32. <option value="GABRIELE">GABRIELE</option>                   
  33. <option value="GIUSEPPE">GIUSEPPE</option>
  34. <option value="DOMENICO">DOMENICO</option>
  35.  
  36. </select> 
  37.  
  38.  
  39. <select size="15" name="opr" multiple>
  40. <option>Seleziona</option>
  41. <option value="GIOVANNI">GIOVANNI/option>                   
  42. <option value="GIACOMO">GIACOMO</option>
  43. <option value="DOROTEO">DOROTEO</option>
  44.  
  45. </select>
  46.  
  47.  
  48. <a href="javascript:insertf1(document.FORM1.tec.value+';'+document.FORM1.opr.value);">            
  49. <img border="0" src="salva.gif" width="84" height="16"> 
This is the hidden field called "tec" form page mother:

Expand|Select|Wrap|Line Numbers
  1. <INPUT TYPE="hidden" NAME="tec" VALUE="-;-">

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Sep 1 '08

re: [Javascript] Hidden fields


For a multiple select element, you need to loop over the options and check which ones are selected.
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#3: Sep 1 '08

re: [Javascript] Hidden fields


Quote:

Originally Posted by acoder

For a multiple select element, you need to loop over the options and check which ones are selected.

Hi acoder, thanks x your reply.

I write this for your suggestion:

Expand|Select|Wrap|Line Numbers
  1.     function options_value_selezionati_join(lista)
  2.     {
  3.         var s = "";
  4.         for(var i = 0; i < lista.options.length; i++)
  5.  
  6.         {
  7.             if(lista.options[i].selected) s += "-" + lista.options[i].value;  
  8.  
  9.         }
  10.  
  11.         return s.substr(1);
  12.  
  13.    }
  14.  
  15. ...
  16.  
  17.             <a href="javascript:(options_value_selezionati_join(document.FORM1.tec)+';'+options_value_selezionati_join(document.FORM1.opr));">  

The values of select it's correct.... but not open alert "OK" and not close window popup and not insert a value in the hidden field called "tec" in form open in the page mother.
.... :(


Can you help me?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Sep 1 '08

re: [Javascript] Hidden fields


All you've done is constructed a string without setting it to anything.
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#5: Sep 1 '08

re: [Javascript] Hidden fields


Quote:

Originally Posted by acoder

All you've done is constructed a string without setting it to anything.

Not working...

Expand|Select|Wrap|Line Numbers
  1. function options_value_selezionati_join(lista)
  2.     {
  3.  
  4.         var s = "";
  5.         for(var i = 0; i < lista.options.length; i++)
  6.  
  7.         {
  8.  
  9.             if(lista.options[i].selected) s += "-" + lista.options[i].value; 
  10.  
  11.  
  12.  
  13.  
  14.         }        
  15.  
  16.                 window.opener.document.options_value_selezionati_join(document.FORM1.tec)=lista;
  17.  
  18.  
  19.  
  20.  
  21.    }
  22.  
  23. ...
  24.  
  25. <a href="javascript:options_value_selezionati_join(document.FORM1.tec); window.close();"> 
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Sep 1 '08

re: [Javascript] Hidden fields


Try:
Expand|Select|Wrap|Line Numbers
  1. window.opener.document.FORM1.tec.value = s;
Can you see where you were going wrong?
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#7: Sep 1 '08

re: [Javascript] Hidden fields


Quote:

Originally Posted by acoder

Try:

Expand|Select|Wrap|Line Numbers
  1. window.opener.document.FORM1.tec.value = s;
Can you see where you were going wrong?

Expand|Select|Wrap|Line Numbers
  1. function options_value_selezionati_join(lista)
  2.     {
  3.  
  4.         var s = "";
  5.         for(var i = 0; i < lista.options.length; i++)
  6.  
  7.         {
  8.  
  9.             if(lista.options[i].selected) s += "-" + lista.options[i].value;
  10.  
  11.           }                 
  12.                 window.opener.document.FORM1.tec.value = s;   
  13.                 alert("OK.");
  14.                 window.close();  
  15.  
  16.  
  17.  
  18.    }
thanks, working but:


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <a href="javascript:options_value_selezionati_join(document.FORM1.tec+';'+document.FORM1.opr);">
  4.  
  5.  
Response whit 'options.lengh' is null or not object... why?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Sep 1 '08

re: [Javascript] Hidden fields


The function is expecting a list/select object, not a string, so pass in one select reference.
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#9: Sep 1 '08

re: [Javascript] Hidden fields


Quote:

Originally Posted by acoder

The function is expecting a list/select object, not a string, so pass in one select reference.

I need pass this string:

<a href="javascript:options_value_selezionati_join(do cument.FORM1.tec+';'+document.FORM1.opr)">

Any idea?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Sep 1 '08

re: [Javascript] Hidden fields


Pass two lists instead of a string and then loop over both and concatenate the string inside the function.
Familiar Sight
 
Join Date: Oct 2007
Posts: 254
#11: Sep 1 '08

re: [Javascript] Hidden fields


Quote:

Originally Posted by acoder

Pass two lists instead of a string and then loop over both and concatenate the string inside the function.

thanks now working, this is solution:

Expand|Select|Wrap|Line Numbers
  1. <script language="Javascript">
  2.  
  3. <!--
  4.  
  5.     function options_value_selezionati_join(lista)
  6.     {
  7.         var s = "";
  8.         for(var i = 0; i < lista.options.length; i++)
  9.         {
  10.             if(lista.options[i].selected) s += "," + lista.options[i].value;
  11.         }
  12.         return s.substr(1);
  13.     }
  14.  
  15.  
  16. function go()
  17. {
  18.     var s1 = options_value_selezionati_join(document.FORM1.tec)
  19.     var s2 = options_value_selezionati_join(document.FORM1.opr);
  20.  
  21.     if(s1 == "" || s2 == "")
  22.     {
  23.         alert("Stop!");
  24.         return;
  25.     }
  26.  
  27.     var s = s1 + ";" + s2;
  28.  
  29.     if(window.opener && window.opener.document.FORM1.tec) window.opener.document.FORM1.tec.value = s;  
  30.     alert("OK."); 
  31.     window.close();
  32. }    
  33.  
  34.  
  35. // -->
  36. </script>
  37.  
  38. ...
  39.  
  40. <a href="#" onclick="go();return false;">
  41.             <img border="0" src="save.gif" width="84" height="16"></a>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#12: Sep 1 '08

re: [Javascript] Hidden fields


Yep, much better. Glad you got it working.
Reply