View Full Version : This code don't work , why?

11-13-2006, 08:12 AM
Date: <br />
<input type="text" name="date" style="width:300px;" class="inputbox" value="document.write(new Date().toLocaleString());" validate="blank"<br />

11-13-2006, 08:55 AM
When embedding JavaScript within HTML you always need the <script> tags. So it might look something like:

Date: <br />
<input type="text" name="date" style="width:300px;" class="inputbox" value="<script type="text/javascript">document.write(new Date().toLocaleString())</script>" validate="blank"><br />

11-13-2006, 09:19 AM
Uhng. Never work, DD. Looks like you've been doing too much server-side coding lately. You will have better luck with this:

Date: <br>
<input type="text" id="date_put" name="date" style="width:300px;" class="inputbox" value="" validate="blank">
<script type="text/javascript">document.getElementById('date_put').value=new Date().toLocaleDateString();</script>

If the input were part of a form, there are better ways to do it and, even without a form, it could be more robust. But, the above will work in most (perhaps all) modern browsers.

11-13-2006, 09:58 AM
Lol what's even scarier is that it just took me a few minutes to even see why my code wouldn't work. I don't think it's that I've been doing too much server side coding lately. More like lack of coding.

11-13-2006, 10:11 AM
I mentioned server side code because that is sort of how one might do something like that in PHP or asp. Glad you got a chuckle out of it.