Skip to main content

Geting controls inside a table in javascript

Often we need to navigate in html container controls like table, div, p etc through JavaScript for doing some type of validation or another task with child of that container control for that we need to
find the all controls (Textbox,Div,H1,Tr,Td etc.) in that container block.

JavaScript have a built-in function named getElementsByTagName by which we can find any type of control inside any container control like to find textbox (as it is a input control type text) we can use getElementsByTagName(input) and further match the type of control if type=text then textbox if type=select then dropdown if type=radio then radio button etc..


radio (Radiobutton)
checkbox (Checkbox)
text (Textbox)
select (Dropdown)

   var tblobj= document.getElementById('<%=tbl.ClientID%>');
   var inputCol = tblobj.getElementsByTagName('input');
   var DivCol = tblobj.getElementsByTagName('div');
   var TdCol = tblobj.getElementsByTagName('Td'); 

An Example:-

function GetAllChilds() {
   var tblobj= document.getElementById('<%=tbl.ClientID%>');
   var tblTxtCol = tblobj.getElementsByTagName('input');
   for (var i = 0; i < tblTxtCollection.length; i++) {
       if (tblTxtCollection[i].type == "text") {
            //Do something with textbox

Popular posts from this blog

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in Following code demonstrating the various ways of implementation.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.