Getting 'cant read if of null' [duplicate] - javascript

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
Here is html:
<div class="nama" id="xxx"></div>
And javascript :
var div = document.getElementById('xxx');
alert(div.id)
Why i am getting mistake?Looks like fine for me

The element was not found.
Most probably because this code was ran before that part of the DOM was ready.
Try placing your code below where the HTML is defined.

window.onload = function() {
var div = document.getElementById('xxx');
alert(div.id)
};

Related

Use document.getElementById when DOM has not rendered [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Vanilla JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it [duplicate]
(10 answers)
window.onload vs document.onload
(9 answers)
Closed 2 years ago.
I have the following jQuery code in a project and I want to remove jQuery library from the project. So I am trying to convert this in to full javascript.
$(document).ready(function () {
$("#price-button").click(priceButtonClicked),
getURLParameter("run") && $("#price-button").click()
});
var priceCell = $("#priceCell");
I tried this using document.getElementById but the issue is my page has not rendered and therefore the elements are not in present at that stage. I have no idea how to overcome this and any help would be greatly appreciated.

How to Fix Uncaught TypeError: Cannot read property 'href' of null [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 4 years ago.
it appears that when I call the if (test.href != "https://www.test.com") it gives me a null value, but I'm not exactly sure why, as I'm expecting it to return the URL
HTML
<p><a id="damn" href="https://www.test.com" target="_self">PC Install</a></p>
JS SCRIPT:
var test= document.getElementById("damn");
if (test.href != "https://www.test.com") {console.log(test)}
Use getAttribute, and it works.
var test = document.getElementById("damn");
if (test.getAttribute("href") != "https://www.test.com") {
console.log(test)
}
<p><a id="damn" href="https://www.test.com" target="_self">PC Install</a></p>
try to reorder your code, and let your script after the 'a' tag.

jQuery global variable, not working [duplicate]

This question already has answers here:
What does $(function() {} ); do?
(6 answers)
Closed 4 years ago.
Why is this simple global variable not working inside a function
var test_1 = "None";
$(function(){
console.log(test_1)
})
I am getting the result Undefined
Check, whether you have jquery or not. I have created a jsfiddle (https://jsfiddle.net/2kudvsr0/) for your code and its printing None (desired output).
Just make sure you are executing your code once jQuery is imported successfully.

Why setting a variable in a iframe works in pure Javascript way but not in Jquery? [duplicate]

This question already has answers here:
Can't append <script> element
(18 answers)
Closed 7 years ago.
I'm trying to add a variable in a iframe, it works in Javascript way but it does not do what I want in Jquery way. Here is a jsfiddle.
Pure javascript way:
var iframeDocument = $('#iframe')[0].contentWindow.document;
var scriptObject = iframeDocument.createElement("script");
scriptObject.type = "text/javascript";
scriptObject.innerHTML = "var variable=9;";
iframeDocument.body.appendChild(scriptObject);
Result is okay in console (variable is set and equals to 9 and iframe context) :
And when I try to change the value of the variable in JQuery:
$('#iframe').contents().find('body').append($('<script type="text/javascript">').html('variable=10;'));
The variable is loaded in parent context and didn't change in iframe!
I know Javascript way is often better than Jquery way but I am simply wondering why this doesn't work as expected.
You are missing </script>, which by the way, you cant't inject like that.
Try this:
$('#iframe').contents().find('body').append($('<script type="text/javascript"><\/script>').html('var variable=10;'));

`getElementById` returning null [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 8 years ago.
A case of document.getElementById returning null. I've read four other questions in SO, and read the reference on MDN, but I have no clue of what's wrong; please help me. Code is as follows:
HTML
<button id="btnButton1">Button1!</button><br>
<button id="btnButton2">Button2!</button><br>
<span id="spanOutPut"></span>
Javascript
getBYid = function(elem) {
return document.getElementById(elem); }
funButton1 = function() { getBYid('spanOutPut').innerHTML = "Button 1 pressed!!" };
funButton2 = function() { getBYid('spanOutPut').innerHTML = "Did you press the Button 2?!" };
getBYid("btnButton1").addEventListener('click', funButton1, false);
getBYid("btnButton2").addEventListener('click', funButton2, false);
I get a TypeError: getBYid(...) is null, on FireBug.
It works when I simply remove the calls to addEventListener from the JS and set onclick inline, as in the following code:
<button onclick="funButton1()">Button1"</button>
What is the difference?
You have to put this after those elements have been loaded into the DOM

Categories