Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  17] [ 3]  / answers: 1 / hits: 12271  / 11 Years ago, wed, december 25, 2013, 12:00:00

This is my first attempt in Javascript, so may be this is fairly easy question.



I need to access row element of a table, each row contains checkbox and two other column. If checkbox is checked, i need to get the id of checkbox.



I made following attempt but element_table.rows returns undefined, therefore i could not proceed. I debugged using Inspect element tool of eclipse and found element_table contains the rows.



Please suggest where I am making a mistake.



Javascript code:



function myfunction3(){
var element_table = document.getElementsByName('collection');
var element_tableRows = element_table.rows;
var selectedTr = new Array();
var data = ;
for(var i =0 ; element_tableRows.length;i++)
{
var checkerbox = element_tableRows[i].getElementsByName('checkmark');
if(checkerbox.checked){
selectedTr[selectedTr.length] = element_tableRows[i].getAttribute(name);
data = data + element_tableRows[i].getAttribute(name);
}
}
var element_paragraph = document.getElementsByName('description');
element_paragraph.innerHTML = data;
}


html code:



<table name=collection border=1px>
<tr name=1>
<td><input type=checkbox name=checkmark></td>
<td>Tum hi ho</td>
<td>Arjit singh</td>
</tr>
<tr name=2>
<td><input type=checkbox name=checkmark></td>
<td>Manjha</td>
<td>Somesh</td>
</tr>
<tr name=3>
<td><input type=checkbox name=checkmark></td>
<td>Ranjhana</td>
<td>A.R Rehman</td>
</tr>
</table>

<input type=button value=Check onclick=myfunction3()>

More From » html

 Answers
2

here's a working version



function myfunction3(){
var element_table = document.getElementsByName('collection');
var element_tableRows = element_table[0].rows;
var selectedTr = new Array();
var data = ;
for(var i =0 ; i < element_tableRows.length;i++)
{
var checkerbox = element_tableRows[i].cells[0].firstChild;
if(checkerbox.checked){
//selectedTr[selectedTr.length] = element_tableRows[i].getAttribute(name); //not sure what you want with this
data = data + element_tableRows[i].getAttribute(name);
}
}
var element_paragraph = document.getElementsByName('description');
element_paragraph.innerHTML = data;
alert(data);
}


http://jsfiddle.net/eZmwy/



jsfiddle for your example, your problem is mainly at when you getElementsByName you need to specify the index, also not that not all getElement methods are available in the table



i would also suggest you learn jQuery, this makes life easier, also not sure why you want to display the data as 1,2,3 the name on the tr... seems pretty strange to me


[#49230] Tuesday, December 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrodfletchers

Total Points: 75
Total Questions: 94
Total Answers: 95

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
jarrodfletchers questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Mon, Jan 21, 19, 00:00, 5 Years ago
Sun, Dec 16, 18, 00:00, 6 Years ago
Sun, Nov 4, 18, 00:00, 6 Years ago
;