Javascript called from within an HTML form question

4 replies
Can you tell me why the value within this form input field does not clear when I click it?

Here's the code...

<head>
<script type="text/javascript"
function change()
{
document.getElementById("myField1").value="";
}
</script>
</head>

<input type="text" id="myField1" name="email" value="Enter Email" style="height:45px;width:463px;font-size:14pt;" onClick="javascript:change()" />

Thanks.

Alex
#called #form #html #inside #javascript #question
  • Profile picture of the author robomedia
    Try closing the script tag <script type="text/javascript">
    {{ DiscussionBoard.errors[10840851].message }}
  • Profile picture of the author robomedia
    or even better try placeholder instead of value and drop that JS
    <input type="text" id="myField1" name="email" placeholder="Enter Email" style="height:45px;width:463px;font-size:14pt;" />
    {{ DiscussionBoard.errors[10840852].message }}
  • Profile picture of the author element121
    Have a look at upgrading to HTML5 also, you can upgrade to use the email type and the required attribute:

    <input type="email" required name="email" placeholder="Enter Email" style="height:45px;width:463px;font-size:14pt;">
    {{ DiscussionBoard.errors[10842375].message }}
  • Profile picture of the author Badaudio
    You must use the placeholder attribute for your form input elements. No need to have javascript there.

    I would also recommend not using inline-css, to prevent clutter. Separate concerns by creating an external stylesheet and writing the following:

    Code:
    input {
        height: 45px;
        width: 463px;
        font-size: 14pt;
    }
    Then your input simply becomes:

    Code:
    <input type="text" id="myField1" name="email" placeholder="Enter Email">
    Much cleaner
    {{ DiscussionBoard.errors[10867179].message }}

Trending Topics