Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > document.all[txtObj] works but document.getElementById[txtObj] fails

Reply
Thread Tools

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

 
 
wk
Guest
Posts: n/a
 
      04-22-2005
hi,
in my code
document.all[txtObj].value works but
document.getElementById[txtObj].value fails.


any ideas why?

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-22-2005


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().


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
botan.guner@gmail.com
Guest
Posts: n/a
 
      04-22-2005
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;
//...;
}

wk wrote:
> hi,
> in my code
> document.all[txtObj].value works but
> document.getElementById[txtObj].value fails.
>
>
> any ideas why?


 
Reply With Quote
 
wk
Guest
Posts: n/a
 
      04-22-2005
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?

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      04-22-2005
"wk" <(E-Mail Removed)> writes:

> 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
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Dietmar Meier
Guest
Posts: n/a
 
      04-22-2005
wk wrote:

> 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[i] = document.getElementById(aIDs[i]);
}
}
else if (document.all) {
// ...
}
}
return aRV;
}

ciao, dhgm

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
InternetExplorer asp.net interop works at one PC, but fails on others Afigels ASP .Net 0 11-16-2004 07:43 PM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM
Query AD using ASP.Net(C#) fails but works in VBScript M. Craig ASP .Net 2 11-19-2003 12:36 AM
Validation works locally but fails on web server Mr Marsh ASP .Net 2 11-04-2003 03:14 PM



Advertisments