Using JavaScript to Find Page Elements.

4. February 2010 17:26

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 >

<body>

<script type="text/javascript">

 

function show(){

var say=document.getElementById("TextBox1");

say.value="Joe";

 

}

 

</script>

    <form id="form1" runat="server">

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

   

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

    </form>

</body>

</html>

 

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"  />

 

 





Comments

4/23/2010 3:58:37 PM #

disc player

Interesting blog. It would be great if you can provide more details about it. Thanks a load!

disc player United States |

4/28/2010 1:02:08 AM #

Rapidshare

I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!

Rapidshare United States |

5/8/2010 6:50:34 PM #

Mariana

I have a joke for you =) What is a cannibal's favorite type of TV show? A celebrity roast.

Mariana United States |

5/20/2010 1:59:41 AM #

Wahdo

Would it be cool to provide some more info on this in the future? Thanks

Wahdo United States |

7/3/2010 11:51:14 AM #

corona salon

Interesting Blog.thanks for sharing information -katie

corona salon United States |

7/6/2010 3:26:27 PM #

medifast diet review

I think most people would agree with your article. I'm going to bookmark this web site so I can come back and read more posts. Keep up the good work!

medifast diet review United States |

7/7/2010 4:02:43 PM #

Lonny Riopel

This is my first time I have visited here. I found a lot of interesting information in your blog. From the tons of comments on your articles, I guess I am not the only one! keep up the great work.

Lonny Riopel United States |



About the author

 The creators of the Web Design Blog have been creating web designs for over 12 years

Page List