Get value from specific cells

Discussion in 'JavaScript' started by yuenli, Jun 27, 2007.

  1. #1
    Hi! I wish to get the value from a cell that specifically click by a user. I attach the script over here. For example, when i click on the first cell it should return "click 1" and if click on second cell should return "click 2". Anyone can help me? Thanks in advance. :)

    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <script type="text/javascript">
    var editwindow = undefined;
    function getcell(e){
    e=window.event;
    var tgt = e.target || e.srcElement;
    while (tgt && !tgt.cells) {tgt = tgt.parentNode;}
    alert(tgt.rowIndex);
    alert(document.getElementById("mytable").cells[tgt.rowIndex].innerHTML); //getting specific row
    }
    </script>
    </HEAD>

    <BODY>
    <p><p>
    <TABLE border= '1' >
    <TBODY id="mytable" onclick="getcell(event)">
    <tr ><td>click 1</td>
    <td >click 2</td>
    <td >click 3</td>
    </tr>
    <tr ><td>click 4</td>
    <td >click 5</td>
    <td >click 6</td>
    </tr>
    </TABLE>
    </BODY>
    </HTML>
     
    yuenli, Jun 27, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function init(){
    
    		var nCells = document.getElementById('dataTable').getElementsByTagName('td');
    		for (i=0; i<nCells.length; i++)
    			{
    			 nCells[i].style.cursor = "pointer";
    			 nCells[i].onclick = function()
    				{
    				 alert(this.firstChild.data);
    				}
    			}
    	}
    
    	onload=init;
    
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    
    </style>
    </head>
    	<body>
    		<table id="dataTable">
    			<tbody>
    				<tr>
    					<td> Click 1 </td><td> Click 2 </td><td> Click 3 </td>
    				</tr>
    				<tr>
    					<td> Click 4 </td><td> Click 5 </td><td> Click 6 </td>
    				</tr>
    			</tbody>
    		</table>
    	</body>
    </html>
    
    Code (markup):
     
    Mike H., Jun 27, 2007 IP