Using JavaScript to Find Page Elements

One use of JavaScript is to locate HTML elements on the page. This is done generally by passing a reference to the element by “name” or by “ID”. This can be used to retrieve values from elements or to assign them values.


The two methods that achieve this affect are:

  1. getElementById()
  2. getElementByName()

An example of finding an element by ID and assigning a value to the element. When the button is clicked, it calls the show method which finds the ID of the test box. Once the element has been captured, it can be further manipulated. In this case, the textbox will be made to have the value of “Joe”.


<html >


<script type=”text/javascript”>


function show(){

var say=document.getElementById(“TextBox1″);






<form id=”form1″ runat=”server”>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>


<input id=”Button1″ type=”button” value=”button” onclick=”show()”/>




The getElementByName() method functions in a similar fashion, but instead of grabbing the ID of an element, it will grab the name attribute. Similar to the item below.

<input name=”NameBox” type=”text” />

Leave a Reply

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