Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Pre defined hyphen as variable name - please help

    Hi all,

    I am relatively new to java; hence I know the basics, but have been thrown into trouble because I am currently working with a cms system that doesnt keep within the correct name conventions (pre defined).

    Within a form, I simply want to set a date to todays date:

    <script type="text/javascript">
    function initdt(mf) {
    var t = new Date;
    mf.01-date.value = t.getDate();
    mf.02-month.value = t.getMonth() + 1;
    mf.03-year.value = t.getFullYear();
    }
    onload="initdt(document.WBForm);"
    </script>

    This works perfect on a form I create, but sadly I must use a form within a pre defined system that uses "01-date"

    Is there a way around this? A simple way of setting 01-date to t.getDate();

    Your help will be very much appreciated

    Regards

    Damon Simpson

  • #2
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solution

    Hi all, Solution to this is:

    <script type="text/javascript">
    function initdt(mf) {
    var t = new Date;
    mf["01-date"].value = t.getDate();
    mf["02-month"].value = t.getMonth() + 1;
    mf["03-year"].value = t.getFullYear();
    }
    onload="initdt(document.WBForm);"
    </script>

    So simple, yet has taken me hours to work out!!!!

    Hope this helps anyone else with a similar problem

    Regards

    Damon Simpson
    www.damonsimpson.com

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    Yeah, problem is that an element name in JavaScript can't begin with a digit *OR* contain a hyphen. So when you have form field names that aren't valid JS element names, you are stuck doing as you did. It's actually a well known problem. Happens all the time with forms designed for use with PHP, where <select multiple> and multiple checkboxes of the same name must have "[]" on the ends of their field names:
    <select multiple name="foo[]">

    So then in JS code you have to do
    Code:
         document.formName.elements["foo[]"].value = "xxx";
    or, as you discovered, the "shortcut" form works as well:
        var form = document.formName
        form["foo[]"].value = "xxx";
    Last edited by Old Pedant; 06-17-2010 at 08:53 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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