Connecting Tech Pros Worldwide Help | Site Map

document.getElementById(myvar) firefox

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 23rd, 2005, 02:33 AM
LargePig
Guest
 
Posts: n/a
Default document.getElementById(myvar) firefox

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, 02:34 AM
LargePig
Guest
 
Posts: n/a
Default 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, 02:34 AM
LargePig
Guest
 
Posts: n/a
Default 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, 02:34 AM
RobG
Guest
 
Posts: n/a
Default 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, 06:25 PM
Jasen Betts
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.