HTML Elements and altering them

With javascript you can alter the different elements within the HTML page, which is great when you are doing things like AJAX and you just want to alter one part of the page and not the whole page.

All you need to do is to get the element that you want to “talk” to and then alter that HTML tags element parts, so lets say that you have a HTML P tag

  <p id="hideme">

you can get that element within a variable for javascript to talk to it via

     var elem = document.getElementById("hideme");

and now you can alter all parts of that HTML P tag, e.g. the style, the inner HTML text (which is the text within the <p>innerHTML</p> tags) which gives you allot more things to do on the page that allows more of a desktop feel to the site over a static page that you need to send back and forth to the server.

Of course you cannot alter any internal parts of the HTML tag that do not exist e.g. for the HTML Input there is no innerHTML because it only has a value (the text within the input box) so if you try and do that some web browsers will error/stop/or just ignore.

Here is the full source code that will alter a HTML P style to red and also the innerHTML, and also a lower one will hide and then show the text within a HTML P tag whilst altering the link to show either “click here to hide me”/”click here to show me”, which once again helps users to now what to do.

<html>
<head>
<script language="javascript">
  function hideme()
  {
     var elem = document.getElementById("hideme");
     var elema= document.getElementById("hidemea");
 
     if (elem.style.display == "none")
     {
	elem.style.display = "block";
	elema.innerHTML = "click to hide me";
     }
     else
     {
	elem.style.display = "none";
	elema.innerHTML = "click to show me";
     }
  }
 
  function start()
  {
    var inputElem = document.getElementById("thewords");
    // you cannot alter the innerHTML of a input tag because it does not have any!!.
    // some browsers will error here because you cannot alter the innerHTML of a input tag
    //inputElem.innerHTML = "somewhere";
    // but you can alter the HTML A tag value because that is what input has
    inputElem.value = "genux was here!";
 
    var para = document.getElementById("paragraph");
    para.innerHTML = "I have changed the text within the paragraph!!!.. yeppy.. and also the colour to red ";
    // you also have access to the style(s) attached to that HTML tag, in this case the color 
    para.style.color = "red";
  }
</script>
</head>
<body>
  <input id="thewords"/>
  <a onclick="javascript:start()">Click me</a>
  <p id="paragraph">
  some text within the paragraph
  </p>
  <p id="hideme">
  hideme after the click below<br/>
  you can alter the style e.g. style.display = value<br/>
  and also alter the innerHTML - as I am altering the HTML A tag below text from hide/show me
  </p>
  <a id="hidemea" onclick="javascript:hideme()">click to hide me</a>
</body>
</html>

with using more javascript on pages, you need to have a way of keeping details of the updates on a page if you browse away from that page and come back which is where some problems could arise. So you just need to freshen up on your HTML tags and what they are capable of doing and have fun 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *