Jump to content

[SOLVED] Hide a text box on drop-down selection


Yesideez

Recommended Posts

Hi, I've got a form that allows the user to enter some data about a movie.

 

I have a few drop-down selection boxes that are populated from the database however the last option in all of them has the value "999" when "Other" is selected. Is there is a way to use Javascript that when "other" is selected a text box appears (from hidden status) and when anything else is selected the text box appears? I need the "other" text box to be hidden on default - is this possible?

 

Here is a sample line of HTML for one of the drop-down boxes:

<tr><td>Release</td><td>: </td><td><select name="selrelease" class="gadtext"><?=$selreleases?><option value="999">Other >>></option></select> <input type="text" name="strrelease" value="" maxlength="40" size="30" class="gadtext" /></td></tr>

 

If anyone is able to post some sample Javascript along with how it would be used or knows of a link to a tutorial or script it'd extremely appreciated.

 

My knowledge of Javascript is barely minimal, many thanks!

Link to comment
Share on other sites

You need to have an onchange event handler that sets the display attribute of the other DOM element to "none".

Thanks for that but any examples you can point me in the right direction please?

 

As I said, my knowledge of Javascript is practically nil.

 

I'm not asking anyone to write the code for me, just examples of how I add the event and what on earth a DOM is and general stuff like that.

Link to comment
Share on other sites

I've written this:

  <script type="text/javascript">
    function checkHide(x,strgad) {
      var opt=document.getElementById(x).value;
      var gad=document.getElementById(strgad);
      if (opt=="999") {gad.value="other";} else {gad.value="moo";}
    }
  </script>

With this being the new HTML:

<tr><td>Audio</td><td>: </td><td><select name="selaudio" id="selaudioid" onchange="checkHide(this.id,straudioid)" class="gadtext"><option value="1">Unknown</option><option value="1">MPEG Layer-3 Decoder</option><option value="2">AVI</option><option value="3">DivX</option><option value="4">WMV</option><option value="5">MOV</option><option value="6">MP4</option><option value="999">Other >>></option></select> <input type="text" name="straudio" id="straudioid" value="" maxlength="40" size="30" class="gadtext" /></td></tr>

I get an error - I've no idea why :/

Link to comment
Share on other sites

OK here is the final code for those wanting to know what I have achieved...

  <script type="text/javascript">
    function checkHide(selgad,strgad) {
      var opt=document.getElementById(selgad).value;
      var strgad2=document.getElementById(strgad);
      if (opt=="999") {strgad2.style.visibility='visible'} else {strgad2.style.visibility='hidden'}
    }
  </script>

This is the HTML...

<tr><td>Audio</td><td>: </td><td><select name="selaudio" id="selaudioid" onchange="checkHide(this.id,'straudioid')" class="gadtext"><option value="1">Unknown</option><option value="1">MPEG Layer-3 Decoder</option><option value="2">AVI</option><option value="3">DivX</option><option value="4">WMV</option><option value="5">MOV</option><option value="6">MP4</option><option value="999">Other >>></option></select> <input type="text" name="straudio" id="straudioid" value="" maxlength="40" size="30" class="gadtext" style="visibility: hidden" /></td></tr>

On loading the page the text gadget is hidden and stays hidden when anything other than "other" is selected in the select gadget. If "other" is selected the text gadget is shown. Select anything else again and it disappears.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.