1

I have built a multipage offline mobile webapp in a single html file. Each form requires a timestamp of when the form loads, since it won't be submitted to the server until the user has connectivity sometime later. I'm using the following script to get the timestamp

<script>window.onload = function() {document.getElementById('date').value = Date();}</script>

I use this in each of the four forms:

                    <input name="date" id="date" type="text">

This works well for the first form that is viewed, but doesn't work for any of the other forms. I've tried everything that I can think of but I am a js novice. Thanks for any help.

1 Answer 1

1

ids must be unique. If you must label multiple elements, use a class instead.

HTML:

<input name="date" class="date" type="text">

JS:

window.onload = function() {
    var currentDate = Date(),
        dateElements = document.getElementsByClassName('date');
    for (var i = 0; i < dateElements.length; ++i) {
        dateElements[i].value = currentDate;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. Thank you. I should have posted a week ago and saved myself a lot of time.

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.