The ID is an attribute that can be added to HTML tags. The value added at this attribute assigns an unique ID for that HTML tag. The "id" can be used to define CSS styles, but can also be used in JavaScript scripts to work with HTML elements and their content. Thus, the id can make a connection between JavaScript and HTML tags.
To reference in a JS script to a HTML tag through its ID, use the following syntax:

- getElementById() is a method of document object, it gets the tag element with the value "id" in its ID attribute.
- - You can use single quotes ('') for id in parentheses.

This syntax ">i>document.getElementById ("id")" returns a reference to the whole HTML element that has thad "id ", define an object containing its attributes and content. To refer to some parts of this object (HTML element), eg. its content or an "style" attribute, you must use specific properties of this object. Below is a list of the most used.

Properties that can be used with getElementById

To understand how to work with "document.getElementById ("id")" and the properties listed above, here are some examples with each of them.

1. attributes

In this example we use a JavaScript function that is executed when we click on a link, it will display an alert window with the name of the seccond attribute of the <a> tag.
<script type="text/javascript"><!--
function test(val) {
  var attr = val.attributes[1].name;
<a href="#" id="id_link" title="test link" onclick="test(this)">A link</a>
Notice that it is not used "document.getElementById('an_id')", but it's used the "this" property. It refers to the current element. But you could also use "document.getElementById('id_link').attributes[1].name" instead of "val.attributes[1].name", the result will be the same.
- This example will display the fallowing link, click to see the result.
A link

2. childNodes

Working with requires knowledge of several properties belonging to it, but here it is presentef a simple example to understand how to use it.
In this example we have a DIV that has some text and a SPAN tag. To see the results of the script with "childNodes", we use a link to call a function that gets the text content of the SPAN tag.
<script type="text/javascript"><!--
function test2() {
  var val2 = document.getElementById("ex2").childNodes[1].childNodes[0].nodeValue;
<div id="ex2">Web courses - <span></span> - lessons, tutorials</div>
<a href="#" onclick="test2()" title="childNodes example">Test 2</a>
- document.getElementById("ex2").childNodes[1].childNodes[0] gets the first element (childNodes[0]) of the second element (childNodes[1]) of the HTML tag with id="ex2"
- Will output:
Web courses - - lessons, tutorials
Test 2

3. getAttribute() and setAttribute()

In this example we use two links that call a JavaScript function, with different parameters.
The function gets an alerts a message with the "title" attribute value of the tag with id="ex3", but when the parameter has the value of 1, this function sets another value for that "tittle" attribute.
<script type="text/javascript"><!--
function test3(nr) {
  if (nr == 1) {
    document.getElementById("ex3").setAttribute('title', 'title changed');
  var val3 = document.getElementById("ex3").getAttribute('title');
<a href="#" id="ex3" title="getAttribute() example" onclick="test3(0)">Link 1</a> - <a href="#" title="Link 2 - setAttribute()" onclick="test3(1)">Link 2</a>
- Click on the first link, the second and than again the first. You'll notice the difference.
Link 1 - Link 2

When you want to change the value of "class" attribute, use ClassName, because IE causes problems with "setAttribute" when it's used to classes.
            object.className = 'value';

4. href

Here is an example that modifies the URL added in the "href" attribute of a link.
<script type="text/javascript"><!--
function test4() {
<a href="" id="ex4" title="href example" target="_blank" onmouseover="test4()">Ajax</a>
- The link has set the "" URL, but when the mouse is over the link, "onmouseover" calls the test4() that changes the value of "href" attribute. When you click on it will open the page: """

You can proceed in the same way FOR "src", which refers to the 'src' attribute (in <img> tags).

5. innerHTML

With innerHTML property you can get the content surrounded by an HTML tag, but it can also replace that content with another one, that can contains also HTML tags.
In this example we have a JS function, a DIV tag and two links. The first link calls the function to display an Alert with the DIV content. The second link transfers a parameter to the same function (its "title" tag value) that indicate it to change the DIV content with that parameter.
<script type="text/javascript"><!--
function test5(vl) {
  if (vl == 0) {
    var val5 = document.getElementById("ex5").innerHTML;
  else {
    document.getElementById("ex5").innerHTML = vl;
<div id="ex5">Web courses - <span></span> - lessons, tutorials</div>
<a href="#" title="innerHTML example" onclick="test5(0)">Show Alert</a> - <a href="#" title="innerHTML - replacement" onclick="test5(this.getAttribute('title'))">Change the DIV content</a>
- Click on the first link and than the second.
Web courses - - lessons, tutorials
Show Alert - Change the DIV content
- when the "document.getElementById("ex5").innerHTML" expresion is in the right side of the "=" character, it gets the tag content, but when it's in the left side of the "=" character, adds into the tag the value written after the '=' (in right side).

6. style

The "style" property can change the graphic style of the elements, using CSS properties. The syntax is: - "cssProperty" and "value" are taken from CSS. Ex.: color (the property) and blue (the value).
The difference appears when are used composed CSS properties, like "font-weight", "margin-top" or "border-top-width", .... In JavaScript disappears the dash '-' character and the next words are written with uppercase first character, so for "font-weight" from CSS in JS is "fontWeight", and "border-top-width" in JS is "borderTopWidth".
<script type="text/javascript"><!--
function test6(nr) {
  if (nr == 0) {
    document.getElementById('ex6').style.display = 'inline';
  else if (nr == 1) {
    document.getElementById('ex6').style.color = '#0101ff';
    document.getElementById('ex6').style.fontWeight = 'bold';
  <a href="#" title="style ex" onmouseover="test6(0)">Link ex6</a>
  - <span id="ex6" style="display:none;" onmouseover="test6(1)">JS getElementById()</span>
- When the test6() function is called with a parameter that has the value of 0, it changes the CSS "display" property of the <span> tag to "inline", making it visible.
- If the parameter is 1, the function sets / changes the value of "color" and "fontWeight" properties.
- In the web page will appear the fallowing link, move the mouse over it, then, over the text.
Link ex6 -

7. value

The value property is used with Form elements, it gets or sets values to fields of the form.
- To use "getElementById('id')" with form elements, they must have assigned an "id".
Here's a simple example that displays an Alert with the text written in a text box.
<script type="text/javascript"><!--
function test7() {
  var val7 = document.getElementById("ex7").value;
<form action="" method="post">
 <input type="text" id="ex7" />
 <input type="button" value="Click" onclick="test7()" />
- The above code will show the fallowing result. Write something in the text field and click the button.

Daily Test with Code Example

Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
Working with getElementById

Last accessed pages

  1. Bubbles3 (8622)
  2. Butterfly Kyodai (6559)
  3. Lucky Balls (391)
  4. Get search data from URL address in JavaScript (1501)
  5. AJAX with POST and PHP (7442)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (47733)
  2. Read Excel file data in PHP - PhpExcelReader (35866)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (35489)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (31998)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (29264)