SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing form field ID to JavaScript function

    I Have an HTML form that looks like this

    Code:
    <form action='<?php echo($_SERVER["PHP_SELF"]) ?>' method='post' name='demoform'>
    
    file:<input type=file name=link1 id=lnk1>
    Date:<input type=text name=me id=ee onFocus="opnexl(this.id,lnk1);">
    file2::<input type=file name=link2 id=lnk2>
    Date:<input type=text name=me id=aa onFocus="opnexl(this.id,lnk2);">
    '
    '
    '
    '
    I have a JavaScript function that needs to do something like this

    Code:
    function opnexl(nam,lnks)
    {
    var str=document.demoform.lnks.value;
    if (str == "")
    {
    document.getElementById(nam).value= "0000-00-00 00:00:00";
    }
    else
    {
    var oExcel = new ActiveXObject("Excel.Application");
    var oWB = oExcel.workbooks.open(str);
    '
    '
    '
    The above code gives this error "document.demoform.lnks.value is null or not an object". I guess the problem is the Id of the link fields are not being passed to the opnexl function. Does anyone know how I can get this program to work?

    Thanks for any help you can give.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Because lnks is a variable, you'll have to use an alternate notation

    var str=document.demoform[lnks].value;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Because lnks is a variable, you'll have to use an alternate notation

    var str=document.demoform[lnks].value;
    I tried it but it didn't work. I still get the same error only now it is

    "document.demoform[...] is null or not an object" .

    Any ideas?

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your passing lnk1 and lnk2 as an object and not as a string. The object is null since it doesn't exist in scope.

    it'd probably be 10x easier to do:

    Code javascript:
    opnexl(this.id,document.demoform.lnk1);

    instead


  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    your passing lnk1 and lnk2 as an object and not as a string. The object is null since it doesn't exist in scope.

    it'd probably be 10x easier to do:

    Code javascript:
    opnexl(this.id,document.demoform.lnk1);

    instead
    Works great!! Thanks very much.


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
  •