/    Sign up×
Community /Pin to ProfileBookmark

Get value from specific cells

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>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 27.2007 — &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;New Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
window.onload=function() {
document.getElementById('mytable').onclick = getcell;
}
var editwindow = undefined;
function getcell(e){
if(!e) {e=window.event;}
var tgt = e.target || e.srcElement;
alert(tgt.firstChild.data);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border='1'&gt;
&lt;tbody id="mytable"&gt;
&lt;tr&gt;
&lt;td&gt;click 1&lt;/td&gt;
&lt;td&gt;click 2&lt;/td&gt;

&lt;td&gt;click 3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;click 4&lt;/td&gt;
&lt;td&gt;click 5&lt;/td&gt;
&lt;td&gt;click 6&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@yuenliauthorJun 27.2007 — HI! Fang Thanks for the help. If i wish to display the cell index together with the value. How can i do that? thanks again.
Copy linkTweet thisAlerts:
@FangJun 27.2007 — alert('text: '+tgt.firstChild.data+'ncell index: '+tgt.cellIndex+'nrow index: '+tgt.parentNode.rowIndex);
Copy linkTweet thisAlerts:
@samanyoluJun 27.2007 — <i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;New Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
window.onload=function() {
var el = document.getElementById('mytable');
el = el.getElementsByTagName('td');
var i = 0, td;
while(td=el[i++]) {
td.onclick = function() { alert(this.firstChild.data); alert(this.parentNode.rowIndex); alert(this.cellIndex);}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border='1'&gt;
&lt;tbody id="mytable"&gt;
&lt;tr&gt;
&lt;td&gt;click 1&lt;/td&gt;
&lt;td&gt;click 2&lt;/td&gt;

&lt;td&gt;click 3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;click 4&lt;/td&gt;
&lt;td&gt;click 5&lt;/td&gt;
&lt;td&gt;click 6&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @yuenli spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...