Select a row from html table and send values onclick of a button
$("#table tr").click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
var value=$(this).find('td:first').html();
alert(value);
});
$('.ok').on('click', function(e){
alert($("#table tr.selected td:first").html());
});
Demo:
http://jsfiddle.net/65JPw/2/
check http://jsfiddle.net/Z22NU/12/
function fnselect(){
alert($("tr.selected td:first" ).html());
}
<html>
<header>
<style type="text/css">
td {border: 1px #DDD solid; padding: 5px; cursor: pointer;}
.selected {
background-color: brown;
color: #FFF;
}
</style>
</header>
<body>
<table id="table">
<tr>
<td>1 Ferrari F138</td>
<td>1 000€</td>
<td>1 200€</td>
</tr>
<tr>
<td>2 Ferrari F138</td>
<td>1 000€</td>
<td>1 200€</td>
</tr>
<tr>
<td>3 Ferrari F138</td>
<td>1 000€</td>
<td>1 200€</td>
</tr>
</table>
<input type="button" id="tst" value="OK" onclick="fnselect()" />
<script>
var table = document.getElementById('table');
var selected = table.getElementsByClassName('selected');
table.onclick = highlight;
function highlight(e) {
if (selected[0]) selected[0].className = '';
e.target.parentNode.className = 'selected';
}
function fnselect(){
var element = document.querySelectorAll('.selected');
if(element[0]!== undefined){ //it must be selected
alert(element[0].children[0].firstChild.data);
}
}
</script>
</body>
</html>
There is only one member with the class 'selected' in this list (NodeList), it is the element[0]. children is a htmlcollection of the 3 <td>'s (inside the <tr>), children[0] ist the first <td> at place [0] and .data is its value. (firstChild is the complete string in quotes.) If you use the console it is easy to find the properties you can use.
You can access the first element adding the following code to the highlight
function
$(this).find(".selected td:first").html()
Working Code:JSFIDDLE