Thank you. Your comment will be posted following moderation.
Please complete the form to post your comment.
{ a resource for web designers & developers }
RSS feed Twitter Flickr YouTube

How to show/hide a hidden input form field using jQuery

This past week I was working on a project at work which included building a request form with multiple fields. It was a rather large form that included a dropdown list with an 'other' option. I wanted to allow users to select the 'other' option which then would show an input field so they can enter a specific description of what that 'other' item is. I didn't want to clutter the form by displaying that additional field by default so I used a little jQuery, thus displaying it dynamically and only when needed.

Update: Thanks to AEXT for the inclusion of my tutorial on their Top Worthwhile Tutorials of the Week!

The XHTML markup:

<form id="myForm" method="post" action="#">
<fieldset>

<label>My name:</label>
<input type="text" name="Name" class="textfield" />

<label>My email address:</label>
<input type="text" name="EmailAddress" value=" " class="textfield" />

<label>Select 'Other' from this dropdown list:</label>
<select class="dropdown" name="Items" id="otherFieldOption">
<option>Select...</option>
<option>Item 1</option>
<option>Item 2</option>
<option>Item 3</option>

<!-- THIS DISPLAYS THE HIDDEN FIELD -->
<option value="otherField">Other</option>
</select>

<!-- BEGIN HIDDEN INPUT -->
<div id="otherField">
<label>This is my other option:</label>
<input type="text" name="otherField" class="textfield" />
</div>
<!-- END HIDDEN INPUT -->

<input type="submit" name="Submit" value="Submit" class="btn" onclick="this.blur();" />
</fieldset>
</form>

The jQuery:

$(document).ready(function() {
  $.viewInput = {
    '0' : $([]),
	//THIS IS THE NAME OF THE DIV WRAPPING THE HIDDEN FIELD
    'otherField' : $('#otherField'),
  };

$('#otherFieldOption').change(function() {
    // HIDES THE INPUT FIELD IF ANOTHER DROPDOWN ITEM IS SELECTED ONCE THE HIDDEN FIELD IS LOADED
    $.each($.viewInput, function() { this.hide(); });
	// SHOWS THE INPUT FIELD ITEM IF SELECTED
    $.viewInput[$(this).val()].show();
  });

});

Pretty it up with some CSS:

/* SET THE HIDDEN INPUT FIELD IN A DIV */

#otherField {
  display: none;
}

/* FORM */

fieldset {
  width: 290px;
  margin: 0;
  padding: 20px;
  background: #f4f4f4;
  border: 1px solid #ddd;
}

label {
  display: block;
  font: bold 1.0em arial,verdana,tahoma,sans-serif;
  color: #444;
  margin: 0 20px 10px 0;
  padding: 0;
  clear: left;
}

.textfield, .dropdown {
  width: 200px;
  font: normal 1.0em arial,verdana,tahoma,sans-serif;
  color: #666;
  margin: 0 0 20px 0;
  padding: 5px 6px;
  background: #fff;
  border: 1px solid #ddd;
}

.dropdown {
  width: 215px;
}

input:focus, select:focus {
  border: 1px solid #ccc;
}

.btn, .btn:focus {
  display: block;
  font: bold 1.0em arial,verdana,tahoma,sans-serif;
  color: #333;
  text-decoration: none;
  margin: 0;
  padding: 3px 5px;
}

Stay well.





Set Phasers on Fun!

Skyrocket Labs is a resource for beginner to advanced web designers & developers, offering original jQuery scripts & design tools, insight into best practices and industry trends and, more importantly, dedicated to celebrating and exchanging with the vast and amazing talent in the designer/developer community. It's about learning, sharing, growing and having a blast.

In the Community

CSS Globe Speckyboy Usability Post Peter Hinton Design Blue Awesome AEXT Smashing Magazine Six Revisions Janko at Warp Speed