In this video we will create a new element.
Javascript code: http://pastebin.com/jnzfkBs8
HTML code: http://pastebin.com/JAfDWmCQ
CSS Code: http://pastebin.com/Nb208UTf
Original source
In this video we will create a new element.
Javascript code: http://pastebin.com/jnzfkBs8
HTML code: http://pastebin.com/JAfDWmCQ
CSS Code: http://pastebin.com/Nb208UTf
Original source
28 responses to “JavaScript Tutorial for Beginners – 36 – Creating a new element”
I keep getting the same text over and over again. How was this problem fixed please????
Hi, EJ
I have a problem with the javascript code. When I have some texts with () or "", the code doesn't run. And after I did that, the createTextNode won't run any more. I don't know why.
I love how he didn't edit out the part where he made a mistake and had to debug
Thanks for making me laugh
if we want to add code instead of text how i we can do it ?
the most amazing channel on youtube. Such an amazing teacher, everything becomes crystal clear after watching your tutorials sir.Thank you!!!
how can we use more than 1 style in function? Can you please guide me!
In the place of element = document.createElement("p"); can we replace paragraph = document.createParagraph("P"); .Will it work?
if i had applied cs to last paragraph element then how can i apply css to append text. please reply
OMG it worked ..!!
ok but yet another question arrives does the sequence matters ?
i mean if
1:1st i'd create a element
2:then text
3: and then append it with the element
4: and then finally append it with the div tag ?
wouldn't be better ? or the sequence doesn't actually matters?
hey, If I click several times. It keeps adding more and more paragraphs. How can I stop that from happening.
Question: Will it work if I declare the main div as class="main" instead of giving it an "id" in HTML, and then calling it in javaScript by getElementsByClassName("main"); instead of getElementById("main");
Thank you so much for your videos! I have been following from the beginning of your series. I do have a question if you wouldn't mind addressing. To practice, I have been going to various resources and was wondering if you could shed some light on how this particular code executes without having a function defined. Thank you!
<!DOCTYPE html>
<html>
<body>
<p>Hello World!</p>
<p>The DOM is very useful.</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method</p>
<p id="demo"></p>
<script>
var x = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) is: ' + x[0].innerHTML;
</script>
</body>
</html>
this is my Html code with inline script for the same code but it still doesn't work
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Batman Fan Club</title>
<link rel="stylesheet" href="styel.css" type="text/css">
<script>
function hereNew(){
var el = document.createElement("p");
var my = document.getElementsById("my");
my.appendChild(el);
var t = document.createTextNode("Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sit sint quasi, exercitationem");
el.appendChild(t);
}
</script>
</head>
<body>
<div id="header">
<h3>BATMAN FAN CLUB</h3> </div>
<div id="navbar">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="about.html">About</a></li>
<li><a href="contact.html">Contact Us</a></li>
<li><a href="forum">Forums</a></li>
</ul>
</div>
<div class="sideright">
<h2>Batman Series</h2>
<p>1989 – The Batman</p>
<p>1991 – The Batman and the Joker</p>
<p>1995 – Batman and Robin</p>
<p>2005 – Batman Begins</p>
<p>2008 – The Dark Knight</p>
<p>2013 – The Dark Knight Rises</p>
<p>2016 – Batman Vs Superman</p>
</div>
<div class="sideright">
<h2>Batman Series</h2>
<p>1989 – The Batman</p>
<p>1991 – The Batman and the Joker</p>
<p>1995 – Batman and Robin</p>
<p>2005 – Batman Begins</p>
<p>2008 – The Dark Knight</p>
<p>2013 – The Dark Knight Rises</p>
<p>2016 – Batman Vs Superman</p>
</div>
<div> <img class="imagesidebar" src="2016-04-01-1459488050-1567099-bvs.jpg"> </div>
<div id="my">
<h1>The Dark Knight Rises</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati asperiores, iure sint quam possimus magnam rerum doloremque impedit alias ea ducimus laborum quia veritatis temporibus quisquam id totam. Eum, deserunt? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores, in, iusto. Temporibus ab iusto provident harum voluptatem, consequuntur maxime doloremque quisquam beatae sapiente error veritatis, amet aspernatur porro omnis, animi!</p>
<h1>Batman Begins</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati asperiores, iure sint quam possimus magnam rerum doloremque impedit alias ea ducimus laborum quia veritatis temporibus quisquam id totam. Eum, deserunt?Lorem ipsum dolor sit amet, consectetur adipisicing elit.
<br>
<br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores, in, iusto. Temporibus ab iusto provident harum voluptatem, consequuntur maxime doloremque quisquam beatae sapiente error veritatis, amet aspernatur porro omnis, animi!</p>
</div>
<div id="my">
<h1>Batman Vs Superman</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati asperiores, iure sint quam possimus magnam rerum doloremque impedit alias ea ducimus laborum quia veritatis temporibus quisquam id totam. Eum, deserunt?</p>
<h1>Batman and Robin</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati asperiores, iure sint quam possimus magnam rerum doloremque impedit alias ea ducimus laborum quia veritatis temporibus quisquam id totam. Eum, deserunt?Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<h1>The Justice League</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores, in, iusto. Temporibus ab iusto provident harum voluptatem, consequuntur maxime doloremque quisquam beatae sapiente error veritatis, amet aspernatur porro omnis, animi!</p>
</div>
<button onclick="hereNew()">Click to Read More</button>
<div id="footer">
<ul>
<li><a href="">About</a></li>
<li><a href="">Careers</a></li>
<li><a href="">Helpdesk</a></li>
<li><a href="">Contact Us</a></li>
</ul> © 2018 Batmanfanclub.com. All Rights Reserved. </div>
</body>
</html>
cud you post the stylesheet too please.
please when i click my read more submit is not working , can someone tell what to do? i need your help
why cant we do main.creatElement() or element.createTextNode() directly?
HELP: code is fine, but when i click again on button, its create another paragraph so on. how can i stop it?
Hello, sir, I have a doubt that, when we click our button, again and again, it show the same paragraph again and again.
I MANAGED TO DO IT BUT WHEN U KEEP CLICKING ON IT I GET THE SAME TEXT OVER AND OVER AGAIN
does it matter what to put in the onclick ?
this is my html code
<!DOCTYPE html>
<html>
<head>
<script src="jscode.js"></script>
<link type="text/css" rel="stylesheet" href=stylesheet.css"
</head>
<body>
<div id="main">
<h2>HANIBAL – THE BATTLE OF CANNE</h1>
<p>
After defeating the Romans at the Battle of Lake Trasimen, Hannibal now moved his forces down south. There was a panic in Rome and Quintus Fabius Maximus was appointed dictator to deal with Hannibal. Fabius, however, refused to meet ith Hannibal in the open field and this strategy became highly unpopular in Rome. Romans were used to attacking, not hifing. The Romans were forced to go on the offensive ater Hannibal positioned his forces between Rome and it's vital grain supply. This setup the epic Battle of Canne. Here Hannibal defeteated the Romans inflicting the worst single day defeat in Rome's history.
</p>
<p>
The Romans were then forced to retreat to Rome. It was a total defeat.
</p>
<h2>ATREMATH OF THE BATTLE OF CANNAE</h1>
<p>
After the horrific defeat at the Battle of Cannae, the Romans reinstituted Fabius avoidance strategy. The Romans no longer refused to meet Hannibal in head on collisions. Rather the Roman army kept a wtxhful eye on Hannibal, but always at the close distance
</p>
<p>
The avoidance strategy worked but it took forever. It took alomost two long decades to drive Hannibal out of Italy. The Roman, adopting a tactic out of Hannibal's playboo, launched an attack against Carthage itself. this forced Hannibal to trvel back to Africa to defend the city. Here he was defeteated at the Battle of Zama.
</p>
  
</div>
<button onclick="newParagrph()">Click if you want to read about another battle </button>
</body>
</html>
========
This is my jscode
function newParagraph() {
var element = document.createElement("p");
var main = document.getElementById("main");
main.appendChild(element);
var text = document.createTextNode("The Battle of Salamis was fought between an alliance of Greek cities and the Persian Empire in 480 BC. The Greeks decisively defeated the Persian navy.");
element.appendChild(text);
}
why does it not work when i click the button
why are you opening with <h2> but closing with </h1> ???
what is div?
A short question: any idea why this doesnt work ?
====HTML====
<html>
<head>
</head>
<body style="background-color:#0B2B2C; color:white;" id="body">
<p>This is the first paragraph of the document</p>
<p>This is the second one</p>
<button onclick="createElement()">Create a new paragraph</button>
</body>
</html>
====JAVASCRIPT====
function createElement(){
var elem=document.createElement('p');
var main=document.getElementById('body');
main.appendChild(elem);
var text=document.createTextNode('the code worked');
elem.appendChild(text);
}
Thanks for the video. You are good at combining real concepts in context of programming. A really rare skill, plus your animated voice is really great. I have a question, when creating a new element a div, how do I apply a style for that div that has just been created? For example, I need to add this styling: #idOfBNewDiv{width: 100px;, height: 200px; z-index: 3; position:absolute;} ……. etc. Thanks.
Thank you so much.Your video help me a lot.
Hi Ej,
In this code there is no stop.Once you click new paragraph with text appears and after that if you keep on clicking the button same text appear again and again .For example if i hit the button three times the new text adds for three time and it keep on going.There must be some stop point. Please guide.