Javascript variable as method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LukeK1980@gmail.com

    Javascript variable as method

    is this possible to do or am I dreaming

    function getsomething(){
    document.write( stuffToGet("myT able","tr","id" ))

    }

    function stuffToGet(elem Id,elemArray,va lueToGet){
    var n, elem =
    document.getEle mentById(elemId ).getElementsBy TagName(elemArr ay)
    var Nelem = elem.length;
    var rS
    for (n=0;n<Nelem,n+ +){
    rS= rS + elem[n].valueToGet;
    }
    return(rS)
    }

    I want to pass the method in a variable but it doesn't seem to work
    for me keep getting and undefined value.
    I get the array fine it is only the method that doesn't seem to work

    Please help thanks
  • RobG

    #2
    Re: Javascript variable as method

    On Jun 12, 6:35 am, LukeK1...@gmail .com wrote:
    is this possible to do or am I dreaming
    >
    function getsomething(){
    document.write( stuffToGet("myT able","tr","id" ))
    >
    }
    >
    function stuffToGet(elem Id,elemArray,va lueToGet){
      var n, elem =
    document.getEle mentById(elemId ).getElementsBy TagName(elemArr ay)
      var Nelem = elem.length;
    var rS
    Initialise rS as a string:

    var rS = '';
      for (n=0;n<Nelem,n+ +){
            rS= rS + elem[n].valueToGet;
    Now concatenation will work. You also want valueToGet to be evaluated,
    so use square bracket notation:

    rS= rS + elem[n][valueToGet];


    Also consider making rS an array:

    var rS = [];

    Then inside the loop:

    rS.push(elem[n][valueToGet]);

    and lastly:

    return rS.join(', ');
    >
    }
           return(rS)
    Return is not a function, there is no need to wrap the return
    expression in brackets.


    --
    Rob

    Comment

    • Dan Rumney

      #3
      Re: Javascript variable as method

      LukeK1980@gmail .com wrote:
      [snip]
      function stuffToGet(elem Id,elemArray,va lueToGet){
      var n, elem =
      document.getEle mentById(elemId ).getElementsBy TagName(elemArr ay)
      var Nelem = elem.length;
      var rS
      for (n=0;n<Nelem,n+ +){
      rS= rS + elem[n].valueToGet;
      }
      return(rS)
      }
      Javascript will interpret this as you requesting a property call
      valueToGet from the object elem[n]

      What you need to use is

      rS = rS + elem[n][valueToGet];

      Comment

      • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

        #4
        Re: Javascript variable as method

        abcLukeK1980@gm ail.com escribió:
        function getsomething(){
        document.write( stuffToGet("myT able","tr","id" ))
        >
        }
        >
        function stuffToGet(elem Id,elemArray,va lueToGet){
        var n, elem =
        document.getEle mentById(elemId ).getElementsBy TagName(elemArr ay)
        var Nelem = elem.length;
        var rS
        for (n=0;n<Nelem,n+ +){
        rS= rS + elem[n].valueToGet;
        }
        return(rS)
        }
        I haven't dived into your code but combining document.write( ) with
        document.getEle mentById() has the drawback that write() can only be used
        before the document is finished and getElementById( ) can only be used
        when the element with such ID is ready. You must be careful with that.


        --
        -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
        -- Mi sitio sobre programación web: http://bits.demogracia.com
        -- Mi web de humor al baño María: http://www.demogracia.com
        --

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Javascript variable as method

          Dan Rumney wrote:
          LukeK1980@gmail .com wrote:
          [snip]
          > for (n=0;n<Nelem,n+ +){
          > rS= rS + elem[n].valueToGet;
          >[...]
          >
          Javascript will interpret this as you requesting a property call
          valueToGet from the object elem[n]
          property _lookup_. Callable objects like Function objects are called instead.
          What you need to use is
          >
          rS = rS + elem[n][valueToGet];
          ACK, or something a little bit more efficient.


          PointedEars
          --
          Anyone who slaps a 'this page is best viewed with Browser X' label on
          a Web page appears to be yearning for the bad old days, before the Web,
          when you had very little chance of reading a document written on another
          computer, another word processor, or another network. -- Tim Berners-Lee

          Comment

          Working...