HTML form element hidden onload javascript - Stack Overflow

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

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.

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>

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/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>
Share Improve this question edited Mar 30, 2013 at 17:47 Dilip Raj Baral 3,0906 gold badges37 silver badges64 bronze badges asked Mar 30, 2013 at 17:39 user2001668user2001668 3
  • why not just set the style with css? – TommyBs Commented Mar 30, 2013 at 17:42
  • because is should be shown if row 16 is not account – mplungjan Commented Mar 30, 2013 at 17:49
  • then if row[16] == "account" give it a specific class or id and override the visibility in the stylesheet for that as well – TommyBs Commented Mar 30, 2013 at 17:52
Add a ment  | 

3 Answers 3

Reset to default 1

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>

You can style it with CSS. This would hide all inputs by default:

input {
    visibility: hidden;
}

There is no onload event for input element, you can hide the element by default using CSS then work on it when the radio box clicked.

Another option is to use onload for body.

<body onload="document.forms[0].otheremail.style.visibility='hidden';" >

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745218643a4617143.html

相关推荐

  • HTML form element hidden onload javascript - Stack Overflow

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

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信