Tag Archives: JavaScript

JavaScript: String trimming and padding

When working with JavaScript in one of my projects I needed to trim strings and pad them. I googled for a solution and many sources I have got the following codes and sharing with you.

Trimming:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//trimming space from both side of the string
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
 
//trimming space from left side of the string
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
 
//trimming space from right side of the string
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

Using Trim Functions:

1
2
3
4
5
//write the code given above
var str = "  black  ";
alert("a" + str.trim() + "b");   //result "ablackb"
alert("a" + str.ltrim() + "b");  //result "ablack b"
alert("a" + str.rtrim() + "b");  //result "a blackb"

Padding:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//pads left
String.prototype.lpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}
 
//pads right
String.prototype.rpad = function(padString, length) {
	var str = this;
    while (str.length < length)
        str = str + padString;
    return str;
}

Using Padding Functions:

1
2
3
var str = "5";
alert(str.lpad("0", 5)); //result "00005"
alert(str.rpad("0", 5)); //result "50000"
share save 120 16 JavaScript: String trimming and padding

Removing table rows using JavaScript

In my last post I have shown how to duplicate a table row. Now if you let the user to duplicate a row then you will look for a solution to remove row dynamically using JavaScript. Use the following function to dynamically remove row from a table. This code is also tested on IE6 and FF2.

/**
 * Removes row of a table. Finds the table with table ID
 *
 * @param targetTableId - Table ID
 * @param targetRowIndex - index of the target row tobe removed
 * @param skipRows - Number of rows to be skipped
 */
 
function removeRow(targetTableId, targetRowIndex, skipRows)
{
     var targetTable = document.getElementById(targetTableId);
     var tableBody = targetTable.tBodies [0];
     var totalRows = tableBody.rows.length;
 
     if(totalRows == skipRows)
     {
          return false;
     }
 
     if(targetRowIndex == undefined || targetRowIndex == "")   
     {       
          targetRowIndex = totalRows - 1;   
     }    
    
     if(tableBody.hasChildNodes())   
     {
          tableBody.removeChild(tableBody.childNodes[targetRowIndex]);   
     }
}
share save 120 16 Removing table rows using JavaScript

Duplicating table rows dynamically

For some days I was looking for a JavaScript solution for duplicating table rows. I know it is simple. But I have made a function which does this very easily. I have tested this code with IE 6.0 and FireFox 2+ browsers.

Here is the JavaScript function:

/**
* This function duplicates a given row
* Author: Mohammad Sajjad Hossain
* Email: info @t sajjadhossain.com, msh134 @t gmail.com
*
* @param targetTableId - ID of the target table
* @param targetRowIndex - index of the row which to be duplicated
* @return void
*/
function duplicateRow(targetTableId, targetRowIndex)
{
     if(targetRowIndex == undefined)
     {
          targetRowIndex = 0;
     }
 
     var targetTable = document.getElementById(targetTableId);
     var tableBody = targetTable.tBodies[0];
     var targetRow = tableBody.getElementsByTagName("tr")[targetRowIndex];
     var newRow = targetRow.cloneNode(true);
     tableBody.appendChild(newRow);
}

Example Code:

<script language="javascript">
     function duplicateRow(targetTableId, targetRowIndex)
     {
          if(targetRowIndex == undefined)
          {
               targetRowIndex = 0;
          }
 
          var targetTable = document.getElementById(targetTableId);
          var tableBody = targetTable.tBodies[0];
          var targetRow = tableBody.getElementsByTagName("tr")[targetRowIndex];
          var newRow = targetRow.cloneNode(true);
          tableBody.appendChild(newRow);
     }
</script>
<table width="500" cellspacing="0" cellpadding="0" id="tblDuplicate" border="1">
     <tr>
          <th>Col1</th>
          <th>Col2</th>
          <th>Col3</th>
          <th>Col4</th>
     </tr>
     <tr>
          <td align="center">row</td>
          <td align="center">row</td>
          <td align="center">row</td>
          <td align="center">row</td>
     </tr>
</table>
<input type="button" value="Duplicate Row" onclick="duplicateRow('tblDuplicate', 1)" />
share save 120 16 Duplicating table rows dynamically