SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Colon Problems

  1. #1
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Colon Problems

    I'm having colon problems! (no seriously)

    I have a form that is part of a commercial e-commerce application, so I can't change the form or field names that post to the cart.

    There are input fields that I am manipulating with JS, and I am having a heck of a time because a couple of the form element names have a colon in them.

    Example:

    Code JavaScript:
    document.frm.5:freeopt.value = variable;


    No matter how I try to escape it, or what we try I get nasty JS errors because of the colon in the field name. The field name is "5:freeopt" i.e.

    Code HTML4Strict:
    <input type="hidden" name="5:freeopt" value="">

    Is there any way to make this JS work without changing the name of the fields?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It isn't just the colon- javascript identifiers can't start with digits, either.
    Don't reference the elements by name in your scripts.

    Use an array index for each element, or a getElementsByTagName nodelist.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could also get around this by using array notation:
    Code:
    document.frm['5:freeopt'].value = variable;
    Personally I don't like accessing form elements using this "shortcut" method via the name attribute as a property of the document, though. I prefer the more rigid DOM methods like getElementsByTagName and getElementsByName.

  4. #4
    phpLD Fanatic bronze trophy dvduval's Avatar
    Join Date
    Mar 2002
    Location
    Silicon Valley
    Posts
    3,627
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I don't belong in this thread, but the title made me laugh.

  5. #5
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestions guys... I got it working and now my colon is not causing any more problems


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
  •