Hi all,
Here is my first programming stuff.
This demo is about implementing search operation on your webpage.
Here is the source code:
HTML and JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Searching the text html table </title>
<style>
table{border: 1px solid #000 }
td{border: 1px solid #000; font-size: 20px; padding: 10px; }
tr:nth-of-type(even){background-color: yellow; }
</style>
</head>
<body>
<h3> Table</h3><p></p>
<table class="tabl" name="tabName1" id="tabId1">
<tr>
<th class="tdClassname"> Name </th>
<th class="tdClassname"> Acc No </th>
<th class="tdClassname"> Branch No </th>
<th class="tdClassname"> Tel </th>
</tr>
<tr>
<td class="tdClassname"> Name 11 </td>
<td class="tdClassname"> Acc No 12 </td>
<td class="tdClassname"> Branch No 13 </td>
<td class="tdClassname"> 14</td>
</tr>
<tr>
<td class="tdClassname"> Name 21 </td>
<td class="tdClassname"> Acc No 22 </td>
<td class="tdClassname"> Branch No 23 </td>
<td class="tdClassname"> 24 </td>
</tr>
<tr>
<td class="tdClassname"> Name 2,1 </td>
<td class="tdClassname"> Acc No 2,2 </td>
<td class="tdClassname"> Branch No 2,3 </td>
<td class="tdClassname"> 2,4 </td>
</tr>
<tr>
<td class="tdClassname"> 4,1 </td>
<td class="tdClassname"> 4,2 </td>
<td class="tdClassname"> 4,3 </td>
<td class="tdClassname"> 4,4 </td>
</tr>
</table>
<br>
<p>Enter values the from table to seach </p><br>
Col 1 text: <input type="text" placeholder="enter text to search" id="txtBoxId1">
Col 2 text: <input type="text" placeholder="enter text to search" id="txtBoxId2">
<input type="button" value="search" onClick="search()">
<script>
function search(){
var txt1;
var txt2;
var inputval_1 = document.getElementById("txtBoxId1").value;
var inputval_2 = document.getElementById("txtBoxId2").value;
inputval_1 = inputval_1.replace(/ {1,}/g,"").trim();
inputval_2 = inputval_2.replace(/ {1,}/g,"").trim();
console.log(inputval_1);
console.log(inputval_2);
var table = document.getElementById("tabId1");
for (var r = 0, n = table.rows.length; r < n; r++) {
/* uncomment following if want to loop trough each cell in the row */
/* for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
alert(table.rows[r].cells[c].innerHTML);
} */
txt1 = table.rows[r].cells[0].innerHTML; /* Column 1 text */
txt2 = table.rows[r].cells[1].innerHTML; /* Column 2 text */
txt1 = txt1.replace(/ {1,}/g,"").trim()
txt2 = txt2.replace(/ {1,}/g,"").trim()
console.log(txt1+" "+txt2)
if(inputval_1 == txt1){
console.log("Column 1 text matched :: "+txt1+" == "+inputval_1);
}
if(inputval_2 == txt2){
console.log("Column 2 text matched :: "+txt2+" == "+inputval_2);
}
}
}
</script>
</body>
</html>
Comments
Post a Comment