Connecting Tech Pros Worldwide Help | Site Map

document.getElementById(myvar) firefox

LargePig
Guest
 
Posts: n/a
#1: Nov 23 '05
I have some code that works in IE:

function simple_example()
{
myarray[0] = "element1of12"
thiselement = document.getElementById(myarray[0])
}

But it does not work in firefox, returns 'null'.

The above is a simplification of my code, and i cannot simply do:

function simple_example()
{
thiselement = document.getElementById("element1of12")
}

I have tried:

function simple_example()
{
myarray[0] = "element1of12"
thiselement = document.getElementById(myarray[0].toString())
}

Anyone have any ideas as to how i can make this work?

Thanks

Marc
LargePig
Guest
 
Posts: n/a
#2: Nov 23 '05

re: document.getElementById(myvar) firefox


Hmm.... Something else is wrong, no problem with the function as such.
document.getElementById(array[0]) is not a problem.

Doh..

LargePig
Guest
 
Posts: n/a
#3: Nov 23 '05

re: document.getElementById(myvar) firefox


> Hmm.... Something else is wrong, no problem with the function as such.[color=blue]
> document.getElementById(array[0]) is not a problem.
>
> Doh..
>[/color]

Yeah....
document.getElementById("something")
works really well in IE even if the element does not have an ID.
Sorry for wasting space on the group with this.....
RobG
Guest
 
Posts: n/a
#4: Nov 23 '05

re: document.getElementById(myvar) firefox


LargePig wrote:[color=blue]
> I have some code that works in IE:
>
> function simple_example()
> {
> myarray[0] = "element1of12"
> thiselement = document.getElementById(myarray[0])
> }
>
> But it does not work in firefox, returns 'null'.[/color]

myarray should be declared as an array prior to trying to use it as one.
myarray and thiselement are globals, are they being affected by events
elsewhere in your code?

Try this test:

<div id="element1of12">element1of12</div>

<script type="text/javascript">

function simple_example()
{
var myarray = ['element1of12'];
var thiselement = document.getElementById(myarray[0]);
if (thiselement){
alert( thiselement && thiselement.nodeName );
}
}

</script>


An element with an ID of 'element1of12' must exist before you try to get
a reference to it.


[...]

--
Rob
Jasen Betts
Guest
 
Posts: n/a
#5: Nov 24 '05

re: document.getElementById(myvar) firefox


On 2005-11-22, LargePig <no@email.com> wrote:[color=blue]
> I have some code that works in IE:
>
> function simple_example()
> {
> myarray[0] = "element1of12"
> thiselement = document.getElementById(myarray[0])
> }
>[/color]
[color=blue]
> Anyone have any ideas as to how i can make this work?[/color]

make sure your document is valid HTML.
make sure the element you are interested in has exactly that ID.

--

Bye.
Jasen
Closed Thread


Similar JavaScript / Ajax / DHTML bytes