Javascript height returning "getElementById('div') is null? - javascript

k this is probably something very easy but for some reason i am getting an error when trying to use javascript to change style.height.
My javascript looks like this:
document.getElementById('div').style.height=100px;
I have also tried
document.getElementById('div').style.height='100px';
document.getElementById('div').style.height='100';
document.getElementById('div').style.height=100;
and get the same error every time.
The error i am getting is:
document.getElementById('div') is null
I get this error in my firebug console. Thanks.

You probably don't have an element with id="div".
Or you're running your code before the body exists.

this have to work fine :
document.getElementById('div').style.height='100px';
please check your html code, and try in the console :
document.getElementById('div').style.height;
and see what returns after and before
(maybe its working and you cant see it, you can check it adding an background-color)

Related

How can I read an CSS variable in :root from an Angular component?

I tried to do it, like this:
this.elementRef.nativeElement.style['--config-use-images'];
and
document.documentElement.style.getPropertyValue('--config-use-images');
Both did not work. The variable is definitely there. I can see it in the dev tools under :root. With the first one I get an undefined and the second one an empty string when I log it in the console.
Try this:
getComputedStyle(document.documentElement).getPropertyValue('--config-use-images')
Or
getComputedStyle(this.elementRef.nativeElement).getPropertyValue('--config-use-images')

javascript if condition not executing all commands

I'm having one little iritating problem. I have simple if condition in javascript code.
It goes something like this:
if (istinito)
{
alert ('123');
document.getElementById('obavestavanje').value="Pobedi "+ime_igraca+"!!!";
kraj=true;
}
Alert apears when istinito=true, but element with id="obavestenje" never get its value, and variable kraj never is set to true. Variable kraj is global variable, and there are no conflicts with other parts of the JS code.
Any ideas why code stops after alert?
Looks like document.getElementById('obavestavanje') is returning null. You are trying to de-reference the null reference by using document.getElementById('obavestavanje').value which results in null pointer exception. If you look into the console, you should see some exception being raised. It is always a good idea to check if the document.getElementById() is returning a valid object before trying to dereference it.
e.g.
if (istinito)
{
alert ('123');
element = document.getElementById('obavestavanje')
if(element){
element.value="Pobedi "+ime_igraca+"!!!";
}
kraj=true;
}
First advice i could give you:
Use more console logging for debugging. Almost any modern browser got a console to debug and other things.
if (istinito) {
console.log("i am here");
}
from that same console you can also execute commands. Those dom manipulations are easily done from the console. just run them and see if it works.
the code:
document.getElementById('obavestavanje').value = "some value"
looks ok. nothing wrong with it. i guess you don't have an element with id "obavestavanje" ?
Looks like your code is okay. And you are sure you have an element by id 'obavestavanje'. Could you please tell what element is it? Is it a button, textbox or someting like that?
Also the String in the "Pobedi "+ime_igraca+"!!!" , what is 'ime_igraca'? Is it a variable and if it is have you defined this variable somewhere?
Or did you mean to give the value "Pobedi ime_igraca !!!" ??
Thanks
Ranis MK

Disabled is undefined javascript error

When I try to do this:
document.getElementById('idOfCheckBox').disabled = disabled; I get error : disabled is undefined.
What can be the reason?
Have you tried to see what document.getElementById('idOfCheckBox') gives you? Is it a HTMLDOMElement with an attribute disabled? If not, maybe you got the wrong element?
Try console.log(document.getElementById('idOfCheckBox')) if you have access to a fairly modern browser.
No such element exists, or
No such variable disabled exists in the current scope.
Pretty self-explanatory.

jquery/js error - DOM Error: DOM Exception 8 - When I .html any variable

I'm receiving a DOM Error: DOM Exception 8 when I try to execute a very simple piece of code. Ive done similar executions a million times and I'm very confused what the problem is.
My Code:
$.post('/dataHandlers/return_stats.php', function(data) {
$('#u').html(data['username']);
var health = data['health'];
$('#health_display').html(health);
$('#energy_display').html(data['energy']);
$('#money_display').html(data['money']);
$('#intelligence_display').html(data['intelligence']);
$('#strength_display').html(data['strength']);
$('#sidebar').find('#level_display').html(data['level']);
}, 'json');
HTML:
<div id='sidebar'>
<div id ="health_display" class="stats_right"></div>
</div>
If I try and .html something that isn't a variable, it works. Whenever I .html any variable whatsoever, I get that error.
Anyone know what this is about? Thanks in advance.
Be sure you're passing a string to html(); if, for example, those are arrays, you can get DOM errors like that.
ie, jQuery("a").html([1,2]) throws the same error; you likely have a type problem.
I wouldn't be surprised if it they're arrays; if you alert([1,2]), it just alerts 1,2, so everything would appear normal.
I've seen a similar problem with the output being interpreted strangely by .html().
Did you try using .text() and seeing what happened?

null exception in javascript

I met with the following error from the following javascript functions, any ideas what is wrong?
BTW: since the whole page is long, I can not post them all here. I am trying to find a small but complete sample to reproduce this issue. Any ideas to debug further to find the root cause?
'Null' is null or not an object
Script:
<script>
$(document).ready(function() {
$("#tag0").tooltip({ effect: 'slide'});
});
</script>
thanks in advance,
George
My first guess was that
$("#tag0")
is returning null, and attempting to call a method on null is probably giving you the error. I have been informed that jQuery won't actually return null if your selector doesn't match anything -- you just get an empty set of results with a length property of 0. If you call a nonexistent method on an object of this result type, perhaps you get the error message you're seeing.
Is it possible there isn't actually an element on the page with ID "tag0"? Should it be a class instead (".tag0" instead of "#tag0")?

Categories

Resources