Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

i need your help. I am new on java-scripting. want to get value of my using javascript.

sample out below below:
Account Code 	Customer name 	        Address
CODE-001 	PARADO, JUVIT, D. 	address 1
CODE-002 	ESCOBAR, FRED    	address 2
CODE-003 	MAGNO, LOENCIO, O 	address 3

what if i click row 1 with code (CODE-001) it will pop-up a messagebox with: "CODE-001 PARADO, JUVIT, D. address 1"


my javascript:

XML
<script type="text/javascript">
    $(document).ready(function () {
        $('#myDataTable tbody tr').dblclick(function () {
          alert(document.getElementById('myDataTable').rows[1].cells[1].childNodes[0].data);

        }).hover(function () {
            $(this).css('background-color', '#ccc');
        }, function () {
            $(this).css('background-color', '#fff');
        });
    });
</script>

how to make this dynamic? --> document.getElementById('myDataTable').rows[1].cells[0].childNodes[0].data


my table codes:
C#
<div id="searchResults">
    <table id="myDataTable" class="display">
      <thead>
        <tr>
            <th>Account Code</th>
            <th>Customer name</th>
            <th>Address</th>
        </tr>
      </thead>

      <tbody>
        @foreach (var Acct in Model)
        {   
            <tr id="@Acct.AcctCode">  
            <td>@Acct.AcctCode</td>
            <td>@Acct.CustName</td> 
            <td>@Acct.CustAddress</td> 
            </tr>  
        }
      </tbody>
   </table>
</div>
Posted
Updated 11-Sep-12 20:58pm
v2

1 solution

Hi,

Replace this line:

JavaScript
alert(document.getElementById('myDataTable').rows[1].cells[1].childNodes[0].data);


with this:

JavaScript
alert($(this).find('td:eq(1)').html());


Some explanation: this refers to your entire row, where the user double clicks. Then using td:eq(1) I find the 2nd cell in your row [remember the index start w/ 0 w/in a row for the cells. The finally .html() gets the name w/in the cell!

Hope this helps!
 
Share this answer
 
Comments
naijeru 12-Sep-12 20:26pm    
thanks so much.. it helps. and it works.. :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900