Connecting Tech Pros Worldwide Help | Site Map

document.getElementById(myvar) firefox

  #1  
Old November 23rd, 2005, 03:33 AM
LargePig
Guest
 
Posts: n/a
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
  #2  
Old November 23rd, 2005, 03:34 AM
LargePig
Guest
 
Posts: n/a

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

  #3  
Old November 23rd, 2005, 03:34 AM
LargePig
Guest
 
Posts: n/a

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.....
  #4  
Old November 23rd, 2005, 03:34 AM
RobG
Guest
 
Posts: n/a

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
  #5  
Old November 24th, 2005, 07:25 PM
Jasen Betts
Guest
 
Posts: n/a

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 Threads
Thread Thread Starter Forum Replies Last Post
Javascript Iframe Refresh Issue with Firefox.... mediagulch answers 11 August 9th, 2007 01:03 PM