Get A Particular Cell Value From Html Table Using Javascript
Solution 1:
To get the text from this cell-
<table><trid="somerow"><td>some text</td></tr></table>
You can use this -
varRow = document.getElementById("somerow");
varCells = Row.getElementsByTagName("td");
alert(Cells[0].innerText);
Solution 2:
functionVcount() {
var modify = document.getElementById("C_name1").value;
var oTable = document.getElementById('dataTable');
var i;
var rowLength = oTable.rows.length;
for (i = 1; i < rowLength; i++) {
var oCells = oTable.rows.item(i).cells;
if (modify == oCells[0].firstChild.data) {
document.getElementById("Error").innerHTML = " * duplicate value";
returnfalse;
break;
}
}
Solution 3:
var table = document.getElementById("someTableID");
var totalRows = document.getElementById("someTableID").rows.length;
var totalCol = 3; // enter the number of columns in the table minus 1 (first column is 0 not 1)//To display all valuesfor (var x = 0; x <= totalRows; x++)
{
for (var y = 0; y <= totalCol; y++)
{
alert(table.rows[x].cells[y].innerHTML;
}
}
//To display a single cell value enter in the row number and column number under rows and cells below:var firstCell = table.rows[0].cells[0].innerHTML;
alert(firstCell);
//Note: if you use <th> this will be row 0, so your data will start at row 1 col 0
Solution 4:
You can also use the DOM way to obtain the cell value: Cells[0].firstChild.data
Read more on that in my post at http://js-code.blogspot.com/2009/03/how-to-change-html-table-cell-value.html
Solution 5:
You can get cell value with JS even when click on the cell:
.......................
<head><title>Search students by courses/professors</title><scripttype="text/javascript">functionChangeColor(tableRow, highLight)
{
if (highLight){
tableRow.style.backgroundColor = '00CCCC';
}
else{
tableRow.style.backgroundColor = 'white';
}
}
functionDoNav(theUrl)
{
document.location.href = theUrl;
}
</script></head><body><tableid = "c"width="180"border="1"cellpadding="0"cellspacing="0">
<% for (Course cs : courses){ %>
<tronmouseover="ChangeColor(this, true);"onmouseout="ChangeColor(this, false);"onclick="DoNav('http://localhost:8080/Mydata/ComplexSearch/FoundS.jsp?courseId=<%=cs.getCourseId()%>');"><tdname = "title"align = "center"><%= cs.getTitle() %></td></tr>
<%}%>
........................
</body>
I wrote the HTML table in JSP. Course is is a type. For example Course cs, cs= object of type Course which had 2 attributes: id, title. courses is an ArrayList of Course objects.
The HTML table displays all the courses titles in each cell. So the table has 1 column only: Course1 Course2 Course3 ...... Taking aside:
onclick="DoNav('http://localhost:8080/Mydata/ComplexSearch/FoundS.jsp?courseId=<%=cs.getCourseId()%>');"
This means that after user selects a table cell, for example "Course2", the title of the course- "Course2" will travel to the page where the URL is directing the user: http://localhost:8080/Mydata/ComplexSearch/FoundS.jsp
. "Course2" will arrive in FoundS.jsp page. The identifier of "Course2" is courseId. To declare the variable courseId, in which CourseX will be kept, you put a "?" after the URL and next to it the identifier.
I told you just in case you'll want to use it because I searched a lot for it and I found questions like mine. But now I found out from teacher so I post where people asked.
The example is working.I've seen.
Post a Comment for "Get A Particular Cell Value From Html Table Using Javascript"