2

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

Community
  • 1
  • 1
rex123
  • 371
  • 3
  • 16
  • 2
    What exactly is the problem? "not working" isn't really a problem description and referencing other questions still leaves us wondering if you're solving the same problem or a different one. If it's the same problem, why are you not using the same solution, `display: none` instead of setting `visibility`? – tvanfosson Dec 24 '12 at 15:44
  • What HTML does that php generate? JavaScript works client-side, so the server-side script is irrelevant to your question; we need the HTML. – David Thomas Dec 24 '12 at 15:44
  • When I check the Album checkbox nothing happens the
    field stays hidden.
    – rex123 Dec 24 '12 at 15:47
  • Anything in the console error log? Have you tried adding "use strict" at the top of the js file to get more errors sent? – Matt Gibson Dec 24 '12 at 15:53
  • cut it out, it's Christmass! – Tomas Dec 24 '12 at 16:10

1 Answers1

3

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);
    });
});
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Wouldn't a ternary form be easier in this situation: `document.getElementById('divAlbum').style.visibility = this.checked ? 'visible' : 'hidden';` (Though I guess it's all down to personal taste...) – David Thomas Dec 24 '12 at 15:55
  • @DavidThomas - just copying his code example with the suggested change. See my update for how I would handle it using jQuery. – tvanfosson Dec 24 '12 at 16:00
  • I would've never considered doing it that way...nice! =) – David Thomas Dec 24 '12 at 16:02
  • I did the suggested change without jQuery, but it didn't have the desired effect. Now the `
    – rex123 Dec 24 '12 at 20:55
  • @tvanfosson - I've copied and pasted your solution into a new file and it works fine. But nothing happens if I try the same thing in my project, something seems to be interfering. What could that be? – rex123 Dec 24 '12 at 22:16