0

(Homework help)

I've tried every little combination and error checking that I can think of, and I still feel stupid being stumped with this one issue. In my HTML, I have an onCLick event for a form that once a user clicks anywhere on the form, the date is supposed to show up in a textbox at the top right of the screen. I can't seem to get my JS to work exactly how I need it to though. I can make it show me the correct value with alerts or document.write's, but it won't populate the text box. Help?

    /*function to get today's date */
function today()
{
    var today = new Date();
    var month = today.getMonth() + 1;
    var day = today.getDate();  
    var year = today.getFullYear();

    if(day<10)
    {
        day= '0' + day;
    } 

    if(month < 10)
    {
        month = '0' + month;
    }

    today = month + '/' + day + '/' + year;

    return today;
}

/*function to get date to load on startup */
function startform()
{
    document.getElementById("formdate").value = today();
}

And my HTML snippet for that form:

<form name="paycheck" onclick="startform()">

I have the JS and HTML files linked up because like I said, I can get the date to show other ways, just not the one I'm trying to get.

EDIT: The input box is as follows:

<input class="date" name="formdate" size="10">
6
  • What about the HTML for the input field? What you've got here looks fine. Make sure you're spelling the field "id" value correctly. Check the console for errors in case something else you didn't post is messing things up. Commented Oct 26, 2013 at 1:16
  • The code you've shown should work fine. And it does. Commented Oct 26, 2013 at 1:18
  • I just dropped my HTML and JS into JS-Bin and it works as intended. It's just not happening in the browser themselves. Nevermind, I got it to work by adding an id field to the HTML side. I thought HTML5 didn't need id tags anymore? Commented Oct 26, 2013 at 1:24
  • are you trying to achieve the result on form click, or should intended for a button click to do same. Commented Oct 26, 2013 at 1:26
  • @Ajay, it was either for an onLoad or onCLick event within the form (for onClick) Commented Oct 26, 2013 at 1:27

2 Answers 2

1

When selecting via id:

document.getElementById("formdate")

Just make sure to have an id assigned to the element:

<input class="date" name="formdate" id="formdate" size="10">

probably just an oversight

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

2 Comments

This was a partly pre-made web page for homework. We've been told that HTML5 doesn't need id's anymore, which is why I didn't think it was that before. Is this not the case? And thanks for the help!
@user2423368 I'm not for sure what he means by ids not being "required" in html5, as they're never really required, but are very useful. The id is the unique identifier of the element, which can easily be selected and used via javascript ( getElementById ), the name is the identifier that is sent to the server when you submit forms.
0

You just missed the id attribute of your textbox:

id="formdate"

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.