SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict Corobori's Avatar
    Join Date
    Jun 2003
    Location
    Concepcion, Chile
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Referencing an element by a variable

    This is something which works fine:

    document.frm("chkD123456").checked=false;

    Now my problem is that I want to make the ("chkD123456") dynamic. I have written something like this

    var var1 = "chkD" + e.name.substring(5,10)
    document.frm(var1).checked=false;

    But this does not work, I have got a message saying that document.frm(....) is not an object

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    try document.frm(eval(var1)).checked=false;
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Addict Corobori's Avatar
    Join Date
    Jun 2003
    Location
    Concepcion, Chile
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a go at it but I am having a problem.

    If you look at http://www.mejoresdatos.cl/b.htm and click the first checkbox with a white background you'll be able to see the problem.

    What I am trying to achieve is that if somebody check the 1st checkbox and the 2nd checkbox is checked to uncheck it, and vice versa

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm assuming document.frm is a reference to your form. Using parentheses here is the problem. frm is not a function or pseduo-construct (like document.all), so to assert properties as strings you need to use a hash-like notation

    document.frm.elements[var1].checked = false;
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Addict Corobori's Avatar
    Join Date
    Jun 2003
    Location
    Concepcion, Chile
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    document.frm.elements[var1].checked = false;
    That way I have got a message saying that document.frm.elements[...]. is null or not an object

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That means that var1 doesn't hold a valid element name. Try alerting var1 just before that line and see if it matches a checkbox name.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Addict Corobori's Avatar
    Join Date
    Jun 2003
    Location
    Concepcion, Chile
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were d.... right, my substring was too short ! it was not e.name.substring(5,10) but e.name.substring(4,10)

    Thanks beetle

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, I see. That's because string characters (as a vector) are zero-based, meaning the first character's index is 0, not 1. Also, FYI, the substring() method doesn't require the 2nd argument in this instance. With only 1 argument, it assumes the end of the string for the 2nd.

    e.name.substring( 4 );

    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •