1

I am trying to put the date and time into a form field onload. Am I doing something wrong here, I can't get it to work. Here is my code:

<HEAD>
<script type="text/javascript">
            function updateData()
            {
                var cl_dt=new Date();
                document.getElementByName("lastpost_cl").value=cl_dt;
            }
</script>
</HEAD>

<BODY onLoad="updateData();">

<form id="FormName" action="updated.php" method="post" name="FormName">
<input id="lastpost_cl" name="lastpost_cl" type="text" size="25" maxlength="255">
2
  • Why not just use jQuery? In the '$(document).ready' jQuery function just set the default value like '$("#lastpost_cl").val = new Date();'. Will handle it easier. Commented Jun 2, 2010 at 19:37
  • Just a footnote: Don't use uppercase tag names like <BODY> and <HEAD>, nor camel case attributes like onLoad - stick with lower case all the way. Also, if this is the full scope of what you're trying to do don't bother with jQuery; it would be like killing an ant hill with the Stark Jericho missile. Commented Jun 2, 2010 at 19:43

2 Answers 2

2

Use getElementById. getElementsByName (There is an 's' after Element!) returns a collection of element(s).

Sign up to request clarification or add additional context in comments.

Comments

1

getElementByName does not exist, at least not cross browser. Use getElementById instead. You already have an ID that is the same, so it will work with just that change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.