Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:30:57+00:00 2026-06-16T10:30:57+00:00

I am new to javascript and I am trying to add a conditional field

  • 0

I am new to javascript and I am trying to add a conditional field to a form. I have looked at these posts:

How do you create a hidden div that doesn't create a line break or horizontal space??
conditional display of html element forms

I have come up with this little example so far which works fine.

<html>
   <head>
       <script language="javascript">

           function cucu(form){
                   if(form.check.checked){
                   document.getElementById("cucu").style.visibility="visible"; 
                   }
                   else{
                          document.getElementById("cucu").style.visibility="hidden";
                  }
          }

              function load()
              {
                      document.getElementById("cucu").style.visibility="hidden";
              }

      </script>
  </head>

  <body onload="load()">
  <form id="form">
      <input type="checkbox" name="check" onclick="cucu(this.form)" >

      <div id="cucu">
      CUCU
      </div>

      </form>
  </body>
<html>

I have tried the same method on a larger side and the only change is that the hidden element is a text field but it just doesnt’ work.

This is the part of the form:

Album:  <INPUT TYPE="checkbox" NAME="checkAlbum" onclick="checkAlbum(this.form)" id="idAlbum">

        <div id="divAlbum" >
        Choisir un album : <input type="text" name="Album" list="Albums">
                <datalist id="Albums">

                    <?php
                        $requete = 'SELECT DISTINCT titreAlbum FROM Album';
                        $resultat = mysqli_query($connexion, $requete);
                            while($row = mysqli_fetch_assoc($resultat)){
                                echo '<option value="'.$row['titreAlbum'].'">';
                        }
                     ?>

                </datalist> <br><br>
        </div>

My head looks as follows:

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html ; charset=UTF-8" />
    <title>BLABLA</title>
    <link rel="stylesheet" type="text/css" href="include/styles.css"> 
    <script language="javascript" >

    function hideElements(){
        document.getElementById("divAlbum").style.visibility="hidden";
    }

    function checkAlbum(form){
        if(form.checkAlbum.checked){
            document.getElementById("divAlbum").style.visibility="visible";
        }else{
            document.getElementById("divAlbum").style.visibility="hidden";
        }
    }

    </script>
</head>

I really don’t know what the problem is. I’ve checked the functions again and again. Any suggestions for the possible error? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:30:59+00:00Added an answer on June 16, 2026 at 10:30 am

    The reason your button is not working is that this refers to the clicked input itself. The form property on the input refers to the value of the form attribute, not the actual form. In reality you don’t need the form attribute, you can simply use this.checked to see if the corresponding input is currently checked.

    Album:  <INPUT TYPE="checkbox" NAME="checkAlbum" onclick="checkAlbum(this)" id="idAlbum">
    
    function checkAlbum(cb){
        if(cb.checked){
            document.getElementById("divAlbum").style.visibility="visible";
        }else{
            document.getElementById("divAlbum").style.visibility="hidden";
        }
    }
    

    Beyond this I would suggest that you consider not applying your JavaScript inline. Keeping your code separate from your mark up improves readability and can help prevent errors. You might consider using the jQuery framework as it will make this sort of thing much easier.

    $(function(){
        $('#idAlbum').on('change', function() { 
            // use change instead of click in case there's a label involved
            $('#divAlbum').toggle(this.checked);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm completely new to javascript, but I'm trying to create a beta form for
I'm trying to add a text field to a form dynamically using javascript. From
Hey there, new to AJAX, JS.. Im trying to add to a Javascript/Ajax search
New to javascript and such, trying to create a loop for fading logos using
I'm fairly new to javascript and I'm trying to make an form for my
I am trying to create a new Element in my javascript code and append
I have been trying to add onclick event to new elements I added with
New to javascript and jQuery, but I'm trying to dynamically add buttons and then
I'm trying to add a set of objects that have been retrieved from a
I am new to Javascript and Google Maps and I am trying to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.