Uncaught TypeError: Cannot read property 'value' of null

155 views
Skip to first unread message

elcaiaimar

unread,
Mar 17, 2015, 11:47:03 AM3/17/15
to django...@googlegroups.com
Hello

I wonder if somebody knows how to solve the next:

I want to send a value from the template to a script and it seems it works but I get an error: Uncaught TypeError: Cannot read property 'value' of null

Below it is my code:

Template:

<input type="hidden" id="idobra" name="variable" value="{{ obra.id }}">

{{ obra.id }} is an object sended from views.py

Javascript:

var idobra = document.getElementById("idobra").value;
console
.log(idobra);

And I don't know what I'm doing wrong. I would like to know if there is another way to send objects directly from django to javascript or if I'm doing well like that, but solving the problem.

Thank you very much!

Alon Nisser

unread,
Mar 18, 2015, 9:17:13 AM3/18/15
to django...@googlegroups.com
Does the browser support getElementById? old browsers don't support it.. If not the your aren't really adding the input tag in the same page where you run the js script. The js by it self is valid (if an element with this id exists, and only one of course)..
Reply all
Reply to author
Forward
0 new messages