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!

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

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

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.

RSS Feed RSS Feed



Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268