Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
erdragerdragsson
Courses Plus Student 5,887 PointsJavascript alert box problem
Hello, i have added alert boxes to my site, but when they pop up every single line of text on the page disapears and the text on the navigation links disapear, and also when i click "Ok" on the 4:th alert box, i think it is,
the header moves down, as some element is pushing it down, heres a picture of it, i hope you can see it clearly
http://i.imgur.com/cPttg2G.png
anyone know the fix for these issues?
erdragerdragsson
Courses Plus Student 5,887 Pointsvar secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
var yourName = prompt("Hello, please type your name!");
alert("Hey " + yourName + " have you ever been wondering how many seconds you have lived?" + " if so, heres a way to find out! press OK and get going ");
var guess = prompt("guess how many seconds you have been alive! :D")
alert("you guessed " + guess + " seconds");
alert("lets find out how many seconds!");
var yearsAlive = prompt("How many years have you been alive?");
var secondsAlive = 365 * secondsPerDay * yearsAlive;
document.write('<center><h1>You have been alive ' + secondsAlive + ' seconds! </h1></center>');
window.setTimeout (
function()
{
alert("Did you guess it right?");
} , 4000);
if(confirm(yearsAlive)) {
var guessmin = prompt("guess how many minutes you have been alive!");
} else {
alert("I cri :S");
}
var minutesAlive = secondsAlive / 60;
alert("you guessed " + guessmin + " minutes");
alert("ready to see if you got it right? :D");
document.write('<center>You have been alive ' + minutesAlive + " minutes! </center>")
2 Answers
Kevin VanderWulp
5,180 Points<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="container" style="text-align: center;">
<!--Content will be appended here-->
</div>
</body>
</html>
var secondsPerMin = 60,
minsPerHour = 60,
hoursPerDay = 24,
daysPerWeek = 7,
weeksPerYear = 52,
secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay,
container = document.getElementById("container");
var yourName = prompt("Hello, please type your name!");
alert("Hey " + yourName + " have you ever been wondering how many seconds you have lived?" + " if so, heres a way to find out! press OK and get going ");
var guess = prompt("guess how many seconds you have been alive! :D")
alert("you guessed " + guess + " seconds");
alert("lets find out how many seconds!");
var secondsAliveText = document.createElement("p"),
yearsAlive = prompt("How many years have you been alive?"),
secondsAlive = 365 * secondsPerDay * yearsAlive;
secondsAliveText.textContent = 'You have been alive ' + secondsAlive + ' seconds!';
container.appendChild(secondsAliveText);
window.setTimeout (
function()
{
alert("Did you guess it right?");
} , 4000);
if(confirm(yearsAlive)) {
var guessmin = prompt("guess how many minutes you have been alive!");
} else {
alert("I cri :S");
}
var minutesAlive = secondsAlive / 60;
alert("you guessed " + guessmin + " minutes");
alert("ready to see if you got it right? :D");
var minutesAliveText = document.createElement("p")
minutesAliveText.textContent = 'You have been alive ' + minutesAlive + ' minutes!';
container.appendChild(minutesAliveText);
Instead of using document.write, I would create a div on the page where you want to insert the text. Give it an id, and store the div element in a variable with document.getElementByID(). Then when you are creating the text via JavaScript you can use document.createElement() to create a new element that you can append to the div with appendChild().
I would also suggest centering your text with CSS instead of the center tag by applying the text-align: center; rule to the container div. You can do it inline like in my example or with an external sheet.
erdragerdragsson
Courses Plus Student 5,887 PointsHey thanks for replying, im new to javascript so im still learning :)
but one thing i wonder, if you could explain what does this code do?
secondsAliveText.textContent = 'You have been alive ' + secondsAlive + ' seconds!';
container.appendChild(secondsAliveText);
why not keep it the way i did ? can you explain what the things you added means, so i can understand them, thanks:)
// erdrag
erdragerdragsson
Courses Plus Student 5,887 Pointsone thing more also, when i added this code, the text wont print out anymore, and the prompts and alerts after yearsalive, does not show up anymore :/
Kevin VanderWulp
5,180 PointsRight, so I included links to the Mozilla Developer Network that explain these functions as well, but I will try to explain some things here.
var container = document.getElementById("container");
I added this to the script will will select an html element with the ID of "container". As I suggested in the post before, you should add a div with an ID where you want your text to be added to the page. This can be anything, but container is an easy to remember ID. You can find the MDN
var secondsAliveText = document.createElement("p"),
yearsAlive = prompt("How many years have you been alive?"),
secondsAlive = 365 * secondsPerDay * yearsAlive;
secondsAliveText.textContent = 'You have been alive ' + secondsAlive + ' seconds!';
container.appendChild(secondsAliveText);
and also
minutesAliveText.textContent = 'You have been alive ' + minutesAlive + ' minutes!';
container.appendChild(minutesAliveText);
I then added this to the script. I added the var secondsAliveText and used the JavaScript function document.createElement() to create a paragraph element. This paragraph element is now contained inside of the variable secondsAliveText. You can now manipulate the element with this variable, which I did when I added secondsAliveText.textContent. This function allows you to set the text inside of the element. I just copied the text you already had in the script and set the text inside of the paragraph element to that. I then used another function called appendChild() which will allow you to append an element inside of another element at the bottom of the parent element that you are appending to.
Kevin VanderWulp
5,180 PointsWith my script, the text will not show up unless you add something to your page like a div element with an id of container. You can change this to anything you want, as long as you change it in the script as well by changing the text inside document.getElementById() function. You want to add this wherever you want the text to show up on your page.
I also had a typo in one of the functions, getElementByID should be getElementById.
I did test my script and it works after fixing that typo so make sure you are following all of my instructions.
Kevin VanderWulp
5,180 PointsAnother thing, you should add the script to your html at the bottom of the body tag in your html so the page has loaded. This will ensure that everything that the script will work with has loaded in the DOM, so everything runs without error.
erdragerdragsson
Courses Plus Student 5,887 Pointsoh okey thanks!, i fixed the problem with the text, and this is my code now, but in the middle of all i get a alert box with no text in it, have a look,
i have added some if confirm statements, and if im correct, they operate as, if the alert box stated is confirmed it will form an action?, try this code and try out yourself, you will see a empty alert box.
var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
var container = document.getElementById("container");
var yourName = prompt("Hello, please type your name!");
alert("Hey " + yourName + " have you ever been wondering how many seconds you have lived?" + " if so, heres a way to find out! press OK and get going ");
var guess = prompt("guess how many seconds you have been alive! :D")
alert("you guessed " + guess + " seconds");
alert("lets find out how many seconds!");
var secondsAliveText = document.createElement("p");
var yearsAlive = prompt("How many years have you been alive?");
var secondsAlive = 365 * secondsPerDay * yearsAlive;
secondsAliveText.textContent = 'You have been alive ' + secondsAlive + ' seconds!';
container.appendChild(secondsAliveText);
window.setTimeout (
function()
{
alert("Did you guess it right?");
} , 4000);
if(confirm(yearsAlive)) {
var guessmin = prompt("guess how many minutes you have been alive!");
} else {
alert("y u leave :(");
}
var minutesAlive = secondsAlive / 60;
alert("you guessed " + guessmin + " minutes");
var minutesAliveText = document.createElement("p")
minutesAliveText.textContent = 'You have been alive ' + minutesAlive + ' minutes!';
container.appendChild(minutesAliveText);
var hoursAliveText = document.createElement("p")
var hoursAlive = minutesAlive / 60;
if(confirm()) {
var guesshour = prompt("guess how many hours you have been alive!");
} else {
alert(":(");
}
alert("you guessed " + guesshour + " hours");
alert("okey lets see if you got everything right!");
hoursAliveText.textContent = 'You have been alive ' + hoursAlive + ' hours!';
container.appendChild(hoursAliveText);
Kevin VanderWulp
5,180 PointsThe confirm() function works just like the alert function, except it returns true if you click ok and false if you click cancel, so if you don't have text in the confirm function just like an alert, it will show an empty dialog box.
erdragerdragsson
Courses Plus Student 5,887 PointsOh okey, that explains it.
Thanks for The help i really appreciate it.
Kind regards.
// erdrag
erdragerdragsson
Courses Plus Student 5,887 Pointsmy point of using this code
if(confirm(yearsAlive)) {
var guessmin = prompt("guess how many minutes you have been alive!");
} else {
alert("y u leave :(");
}
was to make it say something if you press cancel, and when "yearsAlive" alert is confirmed, it will show the "guessmin" prompt, is that what its doing?
and how can i make this code underneath, the same as the one above, do i have to do "if(confirm(guessmin))" so the prompt will show when the "guessmin" is confirmed, or am i out in nowhere, when im thinking like this?
if(confirm()) {
var guesshour = prompt("guess how many hours you have been alive!");
} else {
alert(":(");
}
Sorry for all the questions, i just want to make sure i get it all,
Samuel Webb
25,370 PointsSamuel Webb
25,370 PointsCould you please show us your code?