dom

+11 william appiah-dankwah · January 28, 2017
i am finding it hard to write code with out document.write properties and methods are not working can some one explain and correct this code?
<html>
<head>
<title></title>
<script type="text/javascript">
var name = document.getElementById("myParagraph");
name.innerText = "i have changed the content";
</script>
</head>
<body>
<p id="myParagraph">sdgdgjhd</p>
</body>
</html>



Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Jay Deshaun · January 31, 2017
You would need a trigger for when the document is done loading where as the script that's executed is looking for the document when it doesn't exist yet.

You could do lots of things for this to work, here's 2 examples:
<!doctype html>
<html>
<head>
<title></title>
<script type="text/javascript">
function docLoaded(){
var name = document.getElementById("myParagraph");
name.innerText = "i have changed the content";
}
</script>
</head>
<body onload='docLoaded();'>
<p id="myParagraph">sdgdgjhd</p>
</body>
</html>

Or you could make the document load then execute the script at the end of the page:
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<p id="myParagraph">sdgdgjhd</p>
<script type="text/javascript">
var name = document.getElementById("myParagraph");
name.innerText = "i have changed the content";
</script>
</body>
</html>
0 william appiah-dankwah · January 31, 2017
Ok I am not by my computer yet but isn't the secound example the same as what I have posted?
0 Jay Deshaun · February 2, 2017
Yes but it is triggered after the page is loaded.

In every site, the browser loads them from top to bottom.
0 william appiah-dankwah · February 3, 2017
I understand it triggers after the page is loaded but I have actually tried it out but it don't work
0 william appiah-dankwah · February 7, 2017
<HTML>
<head>
<title></title>
<script type="text/JavaScript">
function car(model, colour, doors)
{ this.car type = model;
 this.car colour = colour;
 this.number of doors = doors;
}
var buyer = new car( VW golf, gray, 5);
</script>


How would you print out this code using the Dom?
0 Jay Deshaun · February 7, 2017
Sorry, I just realize that it's suppose to be a innerHTML not innerText.

And as for the last code you would need to have some sort of idea of how you want it displayed in HTML.

I could just print them out onto the screen any way like
document.innerHTML = JSON.stringify(object);
0 william appiah-dankwah · February 8, 2017
That will only work with strings
0 william appiah-dankwah · February 8, 2017
<HTML>
<head>
<title></title>
<script type="text/JavaScript">
function car( model, colour, doors)
{ this.cartype = model;
this.carcolour = colour;
this.numberofdoors = doors;
}
var buyer = new car(VW golf, gray, 5);
document.innerHTML=Json.stringify(buyer.cartype);
</script>
</head>
<body onload="car()">


did you write the code like this couse it's not working?
0 Jay Deshaun · February 10, 2017
Try this and read the comments:


function car( model, colour, doors)
{
this.cartype = model;
  this.carcolour = colour;
  this.numberofdoors = doors;
}
var buyer = new car('VW golf', 'gray', 5); //Strings not variables
document.innerHTML = JSON.stringify(buyer); //spelt JSON wrong
0 william appiah-dankwah · February 14, 2017
I have put JSON in capitals it still don't work?
  • 1
  • 2

Javascript

122,755 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators