HTML form element hidden onload javascript

10,979

How about I give the radio and field an ID - actually less important now I wrote a script that use getElementsByName...

DEMO

<?php $hide = $row[16]=='account'; ?>
<style>
<?php if ($hide) { echo '#otheremail {visibility:hidden;}'; ?>
</style>
<script>
 window.onload=function() {
   var rads = document.getElementsByName("gravatarradios");
   rads[0].onclick=rads[1].onclick=function() {
     this.form.otheremail.style.visibility=(this.value=="Use")?'hidden':'visible';
   }
   if (<?php echo $hide; ?>) rads[0].click(); else rads[1].click();
 }
 </script>

HTML

<form action="profile_edit_gravatar.php" method="post">
<label class="radio" for="gravatarradios1">
    <input type="radio" name="gravatarradios" id="gravatarradios1" value="Use">
    Use     </label>
<label class="radio" for="gravatarradios2">
    <input type="radio" name="gravatarradios" id="gravatarradios2" value="Use other">
    Use another email:
</label>
<input type="email" name="otheremail" id="otheremail" placeholder="Gravatar email address">
</form>
Share:
10,979
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    Trying to get a HTML text input to be hidden on load, but then if a radio button is checked then the form will show.

    http://pastie.org/private/ah39ul5h4jtmlntkbmmda

    I've got it working sort of, I just need the field to be hidden on page load.

    <form action="profile_edit_gravatar.php" method="post">
        <label class="radio inline">
            <input type="radio" <?php if($row[16]=='account') {echo ' checked';}?> onload="this.form.otheremail.style.visibility='hidden';" onclick="
                if (this.checked) {
                this.form.otheremail.style.visibility='hidden';
                } else {
                this.form.otheremail.style.visibility='visible';
                }
                return true;
            ">
            Use <?php echo $row[3];?>
        </label>
        <input type="text" name="otheremail">
    </form>