Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a row from html table and send values onclick of a button

I have HTML table where I need to select a row and send its first cell ID to a button and onclick of the button send the selected value to a function in Javascript. How can I achieve this?

test.html :

<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()" /> 

test.js :

var table = document.getElementById('table'),     selected = table.getElementsByClassName('selected'); table.onclick = highlight; function highlight(e) {     if (selected[0]) selected[0].className = '';     e.target.parentNode.className = 'selected'; } function fnselect(){ var $row=$(this).parent().find('td');     var clickeedID=$row.eq(0).text();     alert(clickeedID); } 

test.css :

td {border: 1px #DDD solid; padding: 5px; cursor: pointer;}  .selected {     background-color: brown;     color: #FFF; } 

This is a fiddle of my problem JSFIDDLE

I need to send the selected row's first cell value to a javascript function. But when the user selects a row and clicks on 'OK' button I should send the value to the function. How to do this?

like image 723
user3201640 Avatar asked Jul 15 '14 05:07

user3201640


People also ask

How to send row data when clicking button using JavaScript?

Approach: We get the row id of the row whose button was clicked using “event.target.parentNode.parentNode.id”. Here, “event. targent” returns the element that triggered the event (clicking the button), which in this case is <input>.

How do I select a row in a table?

Click the left border of the table row. The following selection arrow appears to indicate that clicking selects the row. You can click the first cell in the table row, and then press CTRL+SHIFT+RIGHT ARROW.

How do I make a row in a table selectable?

To make the full row selectable, you should add the link to the <tr> instead of to individual table cells. Since you're not allowed to place an <a> tag around a table row, you'll have to think a little bit outside the box to solve this puzzle.


2 Answers

$("#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/

like image 69
RGS Avatar answered Sep 20 '22 16:09

RGS


You can access the first element adding the following code to the highlight function

$(this).find(".selected td:first").html()

Working Code:JSFIDDLE

like image 43
Sunil Hari Avatar answered Sep 22 '22 16:09

Sunil Hari