0

Within a function, I am creating a new variable that contains an HTML element. The variable declaration works fine in Firefox for example, however it comes up "undefined" in Google Chrome.

function updateName(num)
{
	name = document.getElementById("id_name_"+num);
}

Edited by Zurompeta: n/a

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Airshow
0

Hard to see why Chrome should not handle this. You could try changing the id's prefix, eg:

function updateName(num)
{
  name = document.getElementById("myRef_"+num);
}

The variable should also be localised:

function updateName(num)
{
  var name = document.getElementById("myRef_"+num);
}

otherwise a global variable is created. Do this instead:

function updateName(num)
{
  return document.getElementById("myRef_"+num);
}

and use the returned value wherever it is needed, or store it non-globally.

It is bad practise to pass data via the global namespace, though this does not explain the difference between Chrome and other browsers.

Airshow

0

It has been reported several times that Google Chrome has some issues with Javascript...
I don`t any too much javascript so I am not able to say for sure...but the browser could be the problem...

0

It turns out I noobed out lol.

"name" and "class" are reserved names that apparently Firefox doesn't mind but Chrome didn't like.

Thanks for all your input (and the tips Airshow) :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.