Welcome

Hello, Welcome to my blog. If you like feel free to refer others
Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Wednesday, 24 April 2013

Use getElementById in Javascript to iterate through table

var SelectedRowsCount = new Array();
var AllRowsCount = new Array();
var HeaderCheckBox = new Array();

function CountCheckedRowsByCol(detailTableName, colNum)
{
    AllRowsCount[colNum] = 0;
    SelectedRowsCount[colNum] = 0;
   
    var detailTable = document.getElementById(detailTableName);
   
    if (detailTable == null)
        return;

    var Rows = detailTable.getElementsByTagName("tr");

    if (colNum == null)
        colNum = 0;

    for (i = 0; i < Rows.length; i++)
    {
        var cols = Rows[i].getElementsByTagName("td");
       
        if (cols.length <= colNum)
            continue;
       
        var chks = cols[colNum].getElementsByTagName("input");
       
        if (chks.length == 0)
            continue;
        if (chks[0].type != "checkbox")
            continue;
       
        if (AllRowsCount[colNum] == 0)
        {
            if (HeaderCheckBox[colNum] == null)
                HeaderCheckBox[colNum] = chks[0];
        }
        else
        {
            if (chks[0].checked)
                SelectedRowsCount[colNum]++;
        }
       
        AllRowsCount[colNum]++;
    }
   
    AllRowsCount[colNum]--;
   
//    alert(AllRowsCount[colNum]);
   
    //CheckHeaderIfAllColChkBoxSelected(colNum);
}

Tuesday, 7 August 2012

Nested user controls and javascript getElementById

This is the code to find control in WebUserControl which nested in your aspx page.
Suppose the popup.ascx's ID is popupControl and the locations.ascx's ID is locationControl
To find "drpLocationList" which in location.ascx we can use the below mentioned code:

var ddlLocation = document.getElementById('<%=popupControl.FindControl("locationControl").FindControl("drpLocationList").ClientID%>');

Wednesday, 27 June 2012

Printing the content of div or panel

In general, we required to print the content of a portion of page. We don't need that whole page will get printed. So here I am going to provide a small java script function that can solve this problem.
Here is the code for print a specific area.

function PrintDiv() {    
var prtContent = document.getElementById('<%=divPrintArea.ClientID%>');   
var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,
toolbar=1,scrollbars=1,status=0');    
WinPrint.document.write(prtContent.innerHTML);    
WinPrint.document.close();   
WinPrint.focus();   
WinPrint.print();   
WinPrint.close();    
}

Javascript function to check integer value

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

Making a button default clickable when enter key is pressed

Many times we want that on pressing enter key the submit button should be automatic click. For example on login page after typing password generally users press enter key and want that submit button will call but nothing happen.
 Here is the JavaScript code for it:


function doClick(buttonName, e) {
            var key;
            if (window.event)
                        key = window.event.keyCode;     //IE
            else
                        key = e.which;     //firefox
            if (key == 13) {   //Get the button the user wants to have clicked
                        var btn = document.getElementById(buttonName);
                        if (btn != null) { //If we find the button click it
                                    btn.click();
                                    event.keyCode = 0
                        }
            }
}

Tuesday, 26 June 2012

How to check if a string contains special character using javascript


Sometimes we need to check if a string contains any special charachter. Ex. name should not contains any special character.

Here is the code for it : 

function IsSpecialChar(strString)
//  check for valid SpecialChar strings
{
    var strValidChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}




Happy Learning......