954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to hidden fields based on selection

I want to know that "how to show hidden fields when a user selects a particular option in the html form"
I want the fields to be hidden first,then when the users selects:

Option A- Particulars fields which have I will create for this option must be displayed.
If
Option B-Particular field which I will create for this option must be displayed.

I want this code to run as soon as the user selects a option.Not on a button click

akshayphp
Newbie Poster
6 posts since Sep 2011
Reputation Points: 7
Solved Threads: 1
 

Something like this (I haven't tested):

items.onchange = function(){

    var element = document.getElementById('elementId');

    //if you have a lot of options you might want to use switch instead.
    if(this.options[this.selectedIndex].value === 'Option A'){
        element.style.display = 'inline';//could be any of the display values.
    }
}
stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

I like the jquery library because your can change you code to be easier to read and look like this:

$('#elementId).change( function() {
if ($('input:radio[name=bar]:checked').val() == 'Option A') {
  $('#element2).show();
}
})


I think you have use a uniqueID for each radio button while you can use the same name to create a radio button group. So the above code would probably look more like:

$('input:radio[name=foo]').change( function() {
if ($('input:radio[name=foo]:checked').val() == 'Option A') {
  $('#element2).show();
}
})


Since you probably don't want this to run until the document is fully loaded It should look more like:

$(function() {
  $('input:radio[name=foo]').change( function() {
  if ($('input:radio[name=foo]:checked').val() == 'Option A') {
    $('#element2).show();
  }
  })
})
svilla
Junior Poster in Training
88 posts since Aug 2010
Reputation Points: 27
Solved Threads: 27
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: