+ Reply to Thread
Results 1 to 4 of 4

Thread: innerHTML

  1. #1
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,055
    Blog Entries
    6

    innerHTML

    innerHTML allows you to change parts of a web page dynamically. In my last tutorial about GET and POST in PHP, I showed how you could use a button and a textbox and then use GET or POST to print that text on the page. However, to do this, it refreshed the page. Using innerHTML, we will do it without refreshing the page. And, in another tutorial, I’ll be showing how to use ajax and innerHTML to be able to do things like pull information from a database and then use innerHTML to show it. But, on with this tutorial.

    Firstly, we need to setup our page with a textbox and a normal button. I have explained some of this in previous tutorials, however, if you do not know how to set these bits up, I suggest you stop and go back over HTML. I have refrained from using a form element, simply because its not needed.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <input name="name" id="name"><br>
    <input name="Submit" value="Submit" type="button">
    </body>
    </html>
    Now, we need to setup the spot where we will put our text. In our javascript code, we will be finding the spot by id, so we need to add an ID to an HTML tag. In this example, we will use a <p> tag with a id of namehere. We’ll also add an onclick event to our button, that will call a (yet to be made) javascript function.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <p id="namehere"></p>
    <input name="name" id="name"><br>
    <input name="Submit" value="Submit"
     onclick="javascript:changename()" type="button"></body>
    </html>
    Now, we need to implement the javascript function. In this function we set the innerHTML property of the namehere id element to “Hello, “ and then add on the value of the name textbox.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
    http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <script type="text/javascript">function changename() { document.getElementById('namehere').innerHTML = "Hello, " + document.getElementById('name').value; } </script>
    <p id="namehere"></p>
    <input name="name" id="name"><br>
    <input name="Submit" value="Submit"
     onclick="javascript:changename()" type="button"></body>
    </html>
    Run the page, and you’ll find that once you enter a name and press submit, the words “Hello, Whatevernnameyouputin” just above the textbox. I will have a live demo up on my codecall subdomain shortly.

    If this tutorial was helpful, please +rep me
    My Site | Questions and Answers | Ask Me: Termana | Last Tutorial: Ajax innerHTML
    If you can keep your head while all around you are losing theirs, you probably have a CD writer on your desktop

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: innerHTML

    Nice work, Termana. +rep!

  3. #3
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    Re: innerHTML

    nice tutorial, nice and easy +rep
    is that the beginning of JS tutorials by termana?

  4. #4
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: innerHTML

    very nice +rep
    looking forward to your ajax version
    The owls are not what they seem...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Prevent XSS in ASP.NET
    By Showstopper in forum Security Tutorials
    Replies: 1
    Last Post: 07-22-2009, 06:46 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts