Results 1 to 5 of 5

Thread: This code don't work , why?

  1. #1
    Join Date
    Nov 2006
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default This code don't work , why?

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

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Hi:
    When embedding JavaScript within HTML you always need the <script> tags. So it might look something like:

    Code:
    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 />

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

    HTML Code:
    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>
    <br>
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •