0

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

Edited by akshayphp: n/a

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by svilla
0

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.
    }
}
0

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 unique ID 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();
  }
  })
})

Edited by svilla: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.