SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Variables

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2000
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Variables

    Hey everyone! I have a simple question to ask. On the following code, what is the proper way to insert a variable at the place specified.


    function test (somevariable) {

    if (document.forms[0].somevariable.checked)
    {
    statements;
    }
    }


    In that code, JavaScript doesn't treat SOMEVARIABLE as a variable. Ideas on what's wrong?
    Noble Varughese
    President of DesiBoyz Network
    http://www.desiboyz.net

  2. #2
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Try This

    Im not exactly sure what you mean but try initializing your variable first.

    Something like this:

    Code:
    var somevariable = "";//If the variable is text
    var somevariable = 0; //If the variable is a number
    
    function test (somevariable) { 
    
    if (document.forms[0].somevariable.checked) 
    { 
    statements; 
    } 
    }
    Wavelan

  3. #3
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Try something like this:

    Code:
    el = document.forms[0][somevariable];
    
    if (el.checked) 
    { 
    statements; 
    } 
    }
    or:

    Code:
    el = eval("document.forms[0]."+somevariable);
    
    if (el.checked) 
    { 
    statements; 
    } 
    }
    aDog

  4. #4
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Welcome back
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2000
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Thanks

    Thanks Arielladog! Your suggestions work.
    Noble Varughese
    President of DesiBoyz Network
    http://www.desiboyz.net


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
  •