Computing Magazine

Hide HTML Fields with Javascript

Posted on the 29 March 2012 by Akahgy

Description:

Hide or show different HTML fields when markup is changed.

Solution:

There are 3 aspects to take into consideration.

First, we have to identify which element will dictate the change. This is done by adding the property onchange or onclick and so on:

<select id=”age” name=”age” onchange=”javascript: hideFields(document.getElementById(‘age’).value)”>

 

Second, we need to identify which fields will suffer from the change. For this we simply add a distinctive id to them:

<div id=”to_hide”>content of div here</div>

 

Last, we have to create the Javascript function:

function hideFields(age){
   var element_to_hide = document.getElementById(“to_hide”);
   if (age>= 18) {
   element_to_hide.style.display = ”;
   } else {
   element_to_hide.style.display = ‘none’;
   }
}

 

The code above hides an element if age value is less than 18.


You Might Also Like :

Back to Featured Articles on Logo Paperblog

These articles might interest you :

  • One Week

    Week

    It's been just over a week since we landed in Tashkent. The trip was uneventful, other than William acting like a typical 17-month old baby and keeping... Read more

    The 15 July 2018 by   Sherwoods
    FAMILY, PARENTING
  • Not Quite Eschatology

    Quite Eschatology

    Realized eschatology, if you’ll pardon my French, is a term that describes the “already/not yet” aspect of the “end of the world. Read more

    The 15 July 2018 by   Steveawiggins
    POLITICS, RELIGION, SOCIETY
  • 8 Lessons From The Field On Building A Premier Brand

    Lessons From Field Building Premier Brand

    Every new business dreams of becoming the premier brand in their space, like Starbucks is to coffee, and Apple is to consumer electronics, but they have no... Read more

    The 15 July 2018 by   Martin Zwilling
    BUSINESS, CAREER
  • Superhero Doll Hopes to Help Young Black Girls Beat Low Self Esteem

    Superhero Doll Hopes Help Young Black Girls Beat Self Esteem

    FirstLadyB | B Inspired By Entertainment News Young black girls battling lo... Read more

    The 15 July 2018 by   Firstladyb
    SPIRITUALITY
  • 5 of the Best Places in Glasgow for Ice Cream

    Best Places Glasgow Cream

    62 total views, 62 views today 5 of the Best Places in Glasgow for Ice Cream Whether it sunny or dull a “pokey hat” will always bring a smile to a face.  Here... Read more

    The 15 July 2018 by   Emma
    DESTINATIONS, DINING OUT, FOOD & DRINK
  • GoGoHares!

    GoGoHares!

    Following the flight of camp dragons, the parade of vivid jumbos and the troupe of panto gorillas in our midst comes an assortment of big-eared, bright-eyed... Read more

    The 15 July 2018 by   Jackscott
    DIARIES, EXPAT, SELF EXPRESSION
  • Brew the Perfect Iced Tea by TE-A-ME

    Brew Perfect Iced TE-A-ME

    Be it any season, staying hydrated is important and what better way to do so than by enjoying a nice cold glass of tea. Yes, make your water interesting by... Read more

    The 15 July 2018 by   Dr.jenifer Sayyed
    LIFESTYLE

Magazine