By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,520 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,520 IT Pros & Developers. It's quick & easy.

Javascript variable as method

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jun 12, 6:35*am, LukeK1...@gmail.com wrote:
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 = '';
* 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
Jun 27 '08 #2

P: n/a
Lu*******@gmail.com wrote:
[snip]
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];
Jun 27 '08 #3

P: n/a
ab**********@gmail.com escribió:
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
--
Jun 27 '08 #4

P: n/a
Dan Rumney wrote:
Lu*******@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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.