• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

how to assign a listbox-label to a hidden field

I need to assign a listbox-label to a hidden field. I know how to get the value from the listbox. For example i use a js-function

<SCRIPT LANGUAGE=javascript>
<!--
function getSelectedValue()
{
iSelected=document.form1.sparte.selectedIndex
lblValue=document.form1.sparte[iSelected].text

return lblvalue
}
//-->
</script>

Here i get my value. But how do i insert this value into a hidden field. I probably need something like onsubmit to trigger the assignment of the value.
Any help appreaciated.
0
Chillipowder
Asked:
Chillipowder
1 Solution
 
newbieMECommented:
Okay so u got ur label value from the listbox..but does that work?? i would normally use
lblValue = document.form1.sparte.options[iSelected].name
or something similar....but i am kinda new to javascripts myself so maybe urs does work...now to insert that value to a hidden field, u first make the hidden field like the following

<input type="hidden" name="banzai" value="">

and then to insert, use javascript command like

document.form1.banzai.value = getSelectedValue();

i hope i got this right....... O.O
0
 
appariCommented:
try this

<SCRIPT LANGUAGE=javascript>
<!--
function getSelectedValue()
{
iSelected=document.form1.sparte.selectedIndex;
lblValue=document.form1.sparte[iSelected].text;

document.form1.hdnConName.value = lblvalue;

return lblvalue
}
//-->
</script>


replace hdnConName with your hidden control name.
0
 
fritz_the_blankCommented:
You just about have it. Let's say that the name of the hidden field is strHidden, then you would set the value like:

document.form1.strHidden.value = getSelectedValue;

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
fritz_the_blankCommented:
I would call the function with the onChange event of the sparte control, that way the hidden field would always be up to date.

Fritz the Blank
0
 
cubrovicCommented:
if you have a
<form name="myform" onsubmit="sub()" ... >
...
<input type="hidden" name="FHide" />
...

</form>

and the code is

function sub() {
var c = getSelectedValue()
document.forms["myform"].elements["FHide"].value = c
}
0
 
ChillipowderAuthor Commented:
I tried the solution with the onchange. But in the source-code of the page i couldnt find the value from the selected index. Somehow its not working.
<SCRIPT LANGUAGE=javascript>
<!--
function getSelectedValue()
{
iSelected=document.form1.sparte.selectedIndex
lblValue=document.form1.sparte[iSelected].text

document.form1.strHidden.value = lblvalue;
return lblvalue
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" class="adinhalt">
<p class="inhalt">Hersteller Dusy ist in folgenden Sparten vertreten:<br>
</p>
<table width="250" border="0" cellspacing="5" cellpadding="0">
  <tr valign="top">
    <td width="93" align="left" class="header">Sparte</td>
  </tr>
 
  <tr>
    <td width="93"><img src="../images/spacer.gif" width="1" height="10"><span class="inhalt">Einrichtung</span></td>
  </tr>
  <tr>
    <td height="1" width="93"></td>
  </tr>
 
  <tr>
    <td width="93"><img src="../images/spacer.gif" width="1" height="10"><span class="inhalt">Kabinettware</span></td>
  </tr>
  <tr>
    <td height="1" width="93"></td>
  </tr>
 
</table>
<form name="form1" method="POST" action="/stopp/admin/herstellersparte_view2.asp?herstkey=2">
  <p><span class="inhalt">Hersteller folgender Sparte </span>
    <select name="sparte" onchange="getSelectedValue()">
      <option selected>Bitte w&auml;hlen</option>
     
      <option value="1" >Kabinettware</option>
     
      <option value="2" >Kabinettzubehör</option>
     
      <option value="3" >Kosmetik</option>
     
      <option value="4" >Kämme</option>
     
      <option value="5" >Bürsten</option>
           
    </select>
    <input type="submit" name="Abschicken" value="zuordnen">
    <br>
    <input type="hidden" name="herstkey" value="2">
    <input type="hidden" name="herstname" value="Dusy">
    <input type="hidden" name="strHidden" value="">
  </p>
  <input type="hidden" name="MM_insert" value="true">
</form>
</body>
</html>
0
 
fritz_the_blankCommented:
Try this:

<HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function getSelectedValue(intSelected)
{
document.form1.strHidden.value=document.form1.sparte[intSelected].text

alert(document.form1.strHidden.value)
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" class="adinhalt">
<p class="inhalt">Hersteller Dusy ist in folgenden Sparten vertreten:<br>
</p>
<table width="250" border="0" cellspacing="5" cellpadding="0">
 <tr valign="top">
   <td width="93" align="left" class="header">Sparte</td>
 </tr>
 
 <tr>
   <td width="93"><img src="../images/spacer.gif" width="1" height="10"><span class="inhalt">Einrichtung</span></td>
 </tr>
 <tr>
   <td height="1" width="93"></td>
 </tr>
 
 <tr>
   <td width="93"><img src="../images/spacer.gif" width="1" height="10"><span class="inhalt">Kabinettware</span></td>
 </tr>
 <tr>
   <td height="1" width="93"></td>
 </tr>
 
</table>
<form name="form1" method="POST" action="/stopp/admin/herstellersparte_view2.asp?herstkey=2">
 <p><span class="inhalt">Hersteller folgender Sparte </span>
   <select name="sparte" onchange="getSelectedValue(this.selectedIndex)">
     <option selected>Bitte w&auml;hlen</option>
     
     <option value="1" >Kabinettware</option>
     
     <option value="2" >Kabinettzubehvr</option>
     
     <option value="3" >Kosmetik</option>
     
     <option value="4" >Kdmme</option>
     
     <option value="5" >B|rsten</option>
         
   </select>
   <input type="submit" name="Abschicken" value="zuordnen">
   <br>
   <input type="hidden" name="herstkey" value="2">
   <input type="hidden" name="herstname" value="Dusy">
   <input type="hidden" name="strHidden" value="">
 </p>
 <input type="hidden" name="MM_insert" value="true">
</form>
</body>
</html>

Fritz the Blank
0
 
fritz_the_blankCommented:
You will just want to get rid of the line:

alert(document.form1.strHidden.value)


I just put that in there so that you will see that the code is working.

Fritz the Blank
0
 
newbieMECommented:
did u take a look at my suggestion??
0
 
newbieMECommented:
oh nevermind..i am a foo
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now