0

I have an element with an ID of BaseGridView in my HTML. If I call a function directly on it, everything works, but if I get that element via document.getElementById() my function call doesn't work.

For example:

BaseGridView.DoSomething();

Everything works fine for me.

But if I do this:

var hd = document.getElementById("HiddenforMainViewID");
var z = document.getElementById(hd.value);
z.DoSomething();

Nothing works and Firefox says "TypeError: z.DoSomething is not a function". The value of hd is "BaseGridView", and z is an actual element.

Can someone please explain to me why this won't work?

EDIT:

Since there is confusion how this:

BaseGridView.DoSomething();

Can even work, I really have no clue. I'm using DevExpress and somehow it is able to do such things. I've been referring to this example.

hopper
  • 13,060
  • 7
  • 49
  • 53
Marguth
  • 191
  • 9
  • Are you sure there isn't any white space inside that value? – tymeJV Nov 08 '13 at 15:46
  • Do you happen to have a **variable** named `BaseGridView`? – James Montagne Nov 08 '13 at 15:47
  • The only way you could access a DOM element such as `BaseGridView` without calling `document.getElement*` is if you created the element with `new`, added it to the DOM and kept a reference to it. Unless you explicitly did that, my guess is that `BaseGridView` is not the same as the DOM element you get. – Scott Mermelstein Nov 08 '13 at 15:51
  • @Teemu im sorry i am not very strong with javascript. I Called `alert(z);` and it resultet in [object HTMLTableElement] so i guessed it has the right object at hand. @JamesMontagne no i dont have. i edited my post regarding to this. – Marguth Nov 08 '13 at 15:51
  • 1
    Actually it was my bad, just read your code too fast : ). I've removed the comment. – Teemu Nov 08 '13 at 15:54
  • @Teemu your delted comment brought me to the answere! i dont know how Devexpress does it but you were right, one time i use BaseGridView as Variable (working) and one time i get the actual Html Element (not working) So all i had to do is to get the variable via its name and call the function on that. (see [this post](http://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name)) – Marguth Nov 08 '13 at 16:07

1 Answers1

0

"TypeError: z.DoSomething is not a function" and "z is an actual element" (that is, an instance of the Element class) means that there is no DoSomething method available in the Element class.

  • Correct. One time i used the variable BaseGridView which Devexpress somehow can provide there, and the other time i try to use the actual HTML Element. This cant work of course. I had to [get the variable by its name](http://stackoverflow.com/questions/1920867/javascript-get-local-variable-dynamicly-by-name-string) - not the HTML-Element - and everything worked fine. – Marguth Nov 08 '13 at 16:12
  • @Marguth Right, `getElementById` returns an instance of `Element` :) –  Nov 08 '13 at 16:16