I Copied a Table and an input where user can search or focus on a row by using input field. But there are 2 issues.
- When user enter a row number, table shows wrong row like if user enters 15, the table shows row number 16.
- I want when user enter a number, then user gets result by pressing "ENTER" key, it is not necessary to click from mouse.
Here is complete code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js">
</script>
<title>Untitled</title>
<style type="text/css">
table{
margin:5px;
}
td{
padding:3px;
}
tr.active{
background-color:green;
color: white;
}
#control{
line-height:20px;
padding:3px;
position:fixed;
top:0;
left:0;
right:0;
background-color:#999955
}
.t-div{
border: 2px solid black;
width: 250px;
height: 350px;
margin: 50px 15px 15px 15px;
}
</style>
</head>
<body>
<div id="control">
Line <input type="text" size="15" id="line" /><button type="button"
class="btn btn-info"> Search </button>
</div>
<div class="t-div" style="overflow-y: auto;">
<table style="overflow-y: auto;">
<tr>
<td>1</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>2</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>3</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>4</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>5</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>6</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>7</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>8</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>9</td>
<td>This is the line 0 of the table</td>
</tr>
<tr>
<td>10</td>
<td>This is the line 0 of the table</td>
</tr>
</table>
</div>
<script type="text/javascript">
var row = $('tr');
var table = $('table');
$('#control button').click(function(){
var w = $('.t-div');
var row = table.find('tr')
.removeClass('active')
.eq(+$('#line').val())
.addClass('active');
if (row.length){
w.scrollTop( row.offset().top - (w.height()/2) );
}
});
</script>
</body>
</html>