document.all[txtObj] works but document.getElementById[txtObj] fails

W

wk

hi,
in my code
document.all[txtObj].value works but
document.getElementById[txtObj].value fails.


any ideas why?
 
M

Martin Honnen

wk wrote:

in my code
document.all[txtObj].value works but
document.getElementById[txtObj].value fails.

document.getElementById is a function you should call in the form
document.getElementById('string-with-element-id')
so throw out the square brackets you have and correctly call
document.getElementById().
 
B

botan.guner

hi wk,

getElementById() gets only one element that u specified with its id i
think, so it must be used something like this.
function foo(txtObj) {//txtObj is the objects id
var val=document.getElementById(txtObj).value;
//...;
}
 
W

wk

the problem is that the the txtObj part in
document.getElementById[txtObj­].value, is coming froma string made of
comma separated name of objects such as 'txtObj,txtObj2,txtABC'. So i
need to split them, and them use them as actual objects IDs. How to go
about it?

document.getElementById(tx­tObj).value syntax fails, as txtObj is a
string at the moment. Can i somehow convert the string or cast it to an
object?
 
L

Lasse Reichstein Nielsen

wk said:
document.getElementById(tx­tObj).value syntax fails, as txtObj is a
string at the moment.

That is not why it fails. The argument to document.getElementById should
be a string. If it fails, it's because the document doesn't contain an
element with that id.

(And please fix your news client to not insert arbitrary "-"'s in your
text).
/L
 
D

Dietmar Meier

wk said:
document.getElementById(tx­tObj).value syntax fails, as txtObj is a
string at the moment.

document.getElementById(), as well as Microsoft's document.all()
indeed do expect a string as (an optional first, in document.all's
case) argument.

If you have a comma separated list of IDs, and want to get an array
of elements as a result, you might use something like:

function getElementsByIds(sCSIDList) {
var aRV = [], aIDs, i, j, o;
if (typeof sCSIDList == "string") {
aIDs = txtObj.split(/,/);
if (document.getElementByID) {
for (i=0; i<aIDs.length; i++) {
aRV = document.getElementById(aIDs);
}
}
else if (document.all) {
// ...
}
}
return aRV;
}

ciao, dhgm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top