why javascript cant find element? [duplicate] - javascript

This question already has answers here:
My Javascript doesn't work when I put a script element in the head to load it from a URL [duplicate]
(3 answers)
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
JS & jQuery can't detect html elements, and say's they are undefined
(4 answers)
Closed 3 years ago.
I don't know why buy javascript say my div is null.
this the javascript code:
var box = document.getElementById("box");
box.style.height= box.innerText;
and this is the HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="classStyle.css">
</head>
</head>
<body onmousemove="search()">
<script src="java.js"></script>
<center>
<div id="header">Fantasy class</div>
<div id="box">50%</div>
<div type=buttom> יצירת ליגה</div>
</center>
</body>
</html>

Your script tag appears before the div id="box". Therefore, it is run before that div is created, and returns null.
To fix this, try moving the script tag under the div.

Related

How do I remove a specific tags from HTML using JS? [duplicate]

This question already has answers here:
Remove a HTML tag but keep the innerHtml
(7 answers)
Remove element by id
(19 answers)
Closed 2 years ago.
<figure>
<p>Hello please remove parent figure tag. I just want the this child p tag.</p>
</figure>
This is the plain Javascript solution for your problem:
<html>
<body>
<figure id='x'><p id='y'>Hello</p></figure>
<script type='text/javascript'>
let a = document.getElementById('x').innerHTML;
document.getElementById('x').remove();
document.write(a);
</script>
</body>
</html>

how to get header tag without using jquery? [duplicate]

This question already has answers here:
how to get <head> content of the current page with jquery or JS
(3 answers)
Closed 3 years ago.
I wanted this code to work without needing to use jquery to get the header tag:
<head>
<script type="text/javascript" src="js/lib/jquery-1.11.1.min.js" ></script>
<script>
$(function(){ $("head").load("header.html") });
</script>
</head>
How's this?
document.getElementsByTagName('head')[0].innerHTML
Or if you are in fact looking for header tags:
document.getElementsByTagName('header')[0].innerHTML

document.getElementById() does not work inside function closure [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
I'm trying to use function closure in my javascript as follows:
In my html file:
<head>
<script src="myscript.js"></script>
</head>
<body>
<section id="mysectionId"></section>
</body>
In myscript.js:
(function() {
var id = document.getElementById('mysectionId');
console.log(id);
}());
However, id seems to equal null. I'm not sure what I've done wrong - does function closure scope exclude globals like 'document'? If so, how come I can still use 'console.log()' inside the function closure?
You're javascript is running before your html loads. Put your script tag after your html content at the very bottom of the body
<head>
// put css here
</head>
<body>
<section id="mysection"></section>
<script src="myscript.js"></script>
</body>

Java script code works only when I place it in the body section [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 6 years ago.
I don't understand why this simple script works only when I place it in the body?
when i place the script within the head or in external JS file it doesn't work.
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Because the element you reference is defined only later (at the time the <head> is processed the browser hasn't processed the markup underneath it yet).

Beginner javascript text display [duplicate]

This question already has answers here:
Code working in jsFiddle but not in browser
(2 answers)
Closed 9 years ago.
I am having a problem with my JavaScript, when I put it into JSFiddle it works fine. But it wont work locally? Here is the fiddle: http://jsfiddle.net/4Ukkz/
var String = "preacher preacher, 5th grade teacher";
document.getElementById('text').innerHTML=String;
So the problem is. In the JSFiddle, the text will display, But locally nothing will display.
Note. It works in the fiddle. but not on my computer
In JSFiddle, your code is automatically wrapped with
window.onload = function() {
// your code
};
This is necessary because in your code, script.js is included above <div id="text"></div>. If you don't wait until that element exists, document.getElementById('text') will return null and the attempt to set innerHTML will fail.
The solution is to wrap your code as above or move your script tag below the div.
May be you don't have a script.js file with the javascript code you pasted in the fiddle?
And move the script element after your body. So your JavaScript code runs after all the elements in the body have been created.
So something like this:
<html>
<title>hi</title>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="text"></div>
</body>
<script type='text/javascript' src='script.js'></script>
</html>

Categories