HighDots Forums  

Javascript variable as method

Javascript JavaScript language (comp.lang.javascript)


Discuss Javascript variable as method in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
LukeK1980@gmail.com
 
Posts: n/a

Default Javascript variable as method - 06-11-2008 , 03:35 PM






is this possible to do or am I dreaming

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

}

function stuffToGet(elemId,elemArray,valueToGet){
var n, elem =
document.getElementById(elemId).getElementsByTagNa me(elemArray)
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

Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Javascript variable as method - 06-11-2008 , 04:07 PM






On Jun 12, 6:35*am, LukeK1... (AT) gmail (DOT) com wrote:
Quote:
is this possible to do or am I dreaming

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

}

function stuffToGet(elemId,elemArray,valueToGet){
* var n, elem =
document.getElementById(elemId).getElementsByTagNa me(elemArray)
* var Nelem = elem.length;
var rS
Initialise rS as a string:

var rS = '';

Quote:
* 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(', ');

Quote:
}
* * * *return(rS)
Return is not a function, there is no need to wrap the return
expression in brackets.


--
Rob


Reply With Quote
  #3  
Old   
Dan Rumney
 
Posts: n/a

Default Re: Javascript variable as method - 06-11-2008 , 04:10 PM



LukeK1980 (AT) gmail (DOT) com wrote:
[snip]
Quote:
function stuffToGet(elemId,elemArray,valueToGet){
var n, elem =
document.getElementById(elemId).getElementsByTagNa me(elemArray)
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];


Reply With Quote
  #4  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Javascript variable as method - 06-12-2008 , 03:02 AM



abcLukeK1980 (AT) gmail (DOT) com escribió:
Quote:
function getsomething(){
document.write(stuffToGet("myTable","tr","id"))

}

function stuffToGet(elemId,elemArray,valueToGet){
var n, elem =
document.getElementById(elemId).getElementsByTagNa me(elemArray)
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.getElementById() 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
--


Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Javascript variable as method - 06-13-2008 , 01:49 PM



Dan Rumney wrote:
Quote:
LukeK1980 (AT) gmail (DOT) 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.

Quote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.