Presentation is loading. Please wait.

Presentation is loading. Please wait.

Moving away from alert() Using innerHTML Using an empty div section

Similar presentations


Presentation on theme: "Moving away from alert() Using innerHTML Using an empty div section"— Presentation transcript:

1 Moving away from alert() Using innerHTML Using an empty div section
JavaScript Moving away from alert() Using innerHTML Using an empty div section

2 Learning Objectives By the end of this lecture, you should be able to:
Learn a much better technique than alert() to output information to a web page using JavaScript's innerHTML command. Learn how to use a blank <div> section as a placeholder for information you wish to output

3 Bye-bye alert() It is time to say goodbye to an old friend. Though the alert() function has served us well, it should not typically be used in the “real world”. At the very least, it should be reserved for only a few particular situations. Some limitations of alert() include – but are not limited to: The alert box can not display HTML markup. The alert box can not display any images. The alert box disappears (along with any information inside) when the user clicks the ‘OK’ button. Information in an existing alert box can not be modified using scripting. Therefore, instead of alert boxes, we are going to learn how to use a different JavaScript command to output content into our web page.

4 Outputting using innerHTML
The following JavaScript code will output the words: Look at me! (in <h1> markup) into a div section called 'main_content': var someText = "<h1>Look at me!</h1>"; document.getElementById("main_content").innerHTML = someText; This innerHTML command is extremely helpful and widely used. However it is very important to note that the innerHTML command replaces anything and everything that was in that section before. So in this case, the above command would replace everything that was previously inside 'main_content' with the words 'Look at me!' Imagine if that main_content div section contained all of the content for your web page… and you have just replaced that entire section with the words "Look at me!". This is clearly not a desirable result. Fortunately, there is an easy fix….

5 Creating an empty <div> section
The fix is a simple one: We will create an empty <div> section whose only job in life is to hold the output of the innerHTML command: <div id="results"> </div> The following JavaScript code will output the words: Look at me! in <h1> markup into our "results" div section: var someText = "<h1>Look at me!</h1>"; document.getElementById("results").innerHTML = someText; The first line of code simply creates a variable that holds a string containing the text we want to insert into our <div> section. The second line inserts the string from the variable 'someText' into the 'results' div section.

6 Creating an empty <div> section
Suppose that we are writing a conversion function of some kind (e.g. convert pounds to kilograms), and we want to output the information into our HTML document. I will start by creating an empty div section like so: <div id="conversionResults"> </div> Again, at the moment, this <div> section is essentially invisible on our page since there is nothing inside it. Instead of using the alert()function to output results like we've been doing up to this point: alert(kilograms); we will instead use innerHTML like so: document.getElementById("conversionResults").innerHTML = kilograms;

7 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Conversions with innerHTML</title> <script type="text/javascript"> function convertKilos() { var pounds = document.getElementById("txtPounds").value; pounds = parseFloat(pounds); var kilos = pounds*2.28; var output = pounds + " pounds equals " + kilos + " kilograms."; document.getElementById("conversionResults").innerHTML = output; } </script> </head> <body> Enter an amount in pounds: <input type="text" id="txtPounds"> <input type="button" value="Convert to kilograms" onclick="convertKilos()" ><p> <div id="conversionResults"> </div> </body> </html>

8 FILE: inner_html_greeting.htm
The steps Here are the steps as I would recommend doing them: Begin by creating an empty <div> section in which you will later output your information. Name this section whatever you like. I use an identifier for the div that indicates what is going inside. For example: 'conversionResults' or 'greetingOutput'. Create a variable to hold the content you wish to output. Place your entire string inside that variable. You may find yourself doing a certain amount of concatenation here. Output that variable into the div section using JavaScript's innerHTML command. FILE: inner_html_greeting.htm

9 (This would make for a good exam question…)
Bug alert!! Recall that the innerHTML command replaces any content that was inside that section before. Suppose that we want to greet the user and print the date. What do you think will happen here? var greeting = "Hello, how are you?" var todaysDate = "Today is " + Date(); document.getElementById("output").innerHTML = greeting; document.getElementById("output").innerHTML = todaysDate; Answer: The second innerHTML command will replace whatever was previously inside the "output" div. Since the second innerHTML command is executed by JavaScript essentially instantly after the first innerHTML command, the visitor to our page will never see the first line (the greeting). (This would make for a good exam question…)

10 FILE: inner_html_greeting_concatenated.htm
Concatenation redux Can you fix this code so that both strings get output into the 'output' div? var greeting = "Hello, how are you?" var todaysDate = "Today is " + Date(); document.getElementById("output").innerHTML = greeting; document.getElementById("output").innerHTML = todaysDate; Answer: var greeting = "Hello, how are you?" + "<br>Today is " + Date() + ". <br>I hope you have a nice day."; FILE: inner_html_greeting_concatenated.htm

11 Another example FILE: innerHTML_conversions.htm


Download ppt "Moving away from alert() Using innerHTML Using an empty div section"

Similar presentations


Ads by Google