To get access to a specific element on a webpage, assign it a unique id and use the document.getElementById method to get a reference to it. To access all elements of a certain type, use the getElementsByTagName method to create a reference to the collection of all the elements of that type.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

SurveyClub

Accessing Web Page Elements

You can get access to a specific element on a webpage by assigning it a unique id and then using the document.getElementById method to create a reference to that element, as in the example is shown below.

<p id="accessme">You can access this text.</p>

<script language="JavaScript">
var bsize = 1;
function changeSize()
{
  var textref = document.getElementsById("accessme");
  if(bsize) textref.style.fontSize = "20px";
  else textref.style.fontSize = "12px";
  bsize = !bsize;
}
</script>

<input type="button" value="Change Text Size"
onclick="changeSize()"></input>

The example above changes the size of the text in the paragraph when you click on the button.

- You may not be familiar with just creating a free-standing button that is not associated with a form. Actually, by default, it is associated with form[0] in the document's forms collection (assuming it's the first form on the webpage).

To access all elements of a certain type on a webpage, use the getElementsByTagName method to create a reference to the collection of all the elements of that type on the webpage, as in the example is shown below.

<p>You can access this text.</p>

<p>And this text.</p>

<p>And any other text in a paragraph.</p>

<script language="JavaScript">
var bsize = 1;
function changeSize()
{
  var coltext = document.getElementsByTagName("p");

  for(var i = 0; i < coltext.length; i++)
  {
    if(bsize) coltext[i].style.fontSize = "20px";
    else coltext[i].style.fontSize = "12px";
  }
  bsize = !bsize;
}
</script>

<input type="button" value="Change Text Size"
onclick="changeSize()"></input>

The example above changes the size of the text in all paragraphs when you click on the button.

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269