Friday, September 25, 2009

Javascript ReplaceAll

In order to replace all occurrences of a particular string or char, you can use the format below:

replace(//g, "");

Lets say there is a variable: var tag = ctl0_Parent:Child:Item;
var itm = tag.replace(":", "_");
will result with itm being equal to: ctl0_Parent_Child:Item
var itm = tag.replace(/:/g, "_");
on the other hand, will result in itm being equal to: ctl0_Parent_Child_Item

Tuesday, September 15, 2009

Dynamically turn on/off Panel with Javascript

1.) Set client side OnChange event on a Button, CheckBox, etc...
  OnChanged="javascriptChangeHandler(this, 'Panel1');"

2.) Include the Panel named in the javascriptChangeHandler call in HTML
  <asp:Panel id="Panel1" Runat="server">
    Content...
  </asp:Panel>

3.) Add the Javascript function to the page or within included .js file
function javascriptChangeHandler (elem, panel) {
  var part = elem.id.substr(0, elem.id.lastIndexOf('_'));
  //Handle part logic to determine if panel should be turned off or on

  if ( part == null ) {
    elem.focus();
    return false;
  } else if ( part == "someIdentity" ) {
    document.getElementById(panel).style.display = "block";
    } else {
      document.getElementById(panel).style.display = "none";
    }
  }
}