/    Sign up×
Community /Pin to ProfileBookmark

Table Row Functions ?

folks

I have a table like below

<table>
<tr onClick=”javascript:abc();”>
<td><input type = checkbox></td>
<td><input type=text name=txt1>abc</td>
<td><input type=text name=txt1>xyz</td>
</tr>
</table>

I need to get the values of all of the ‘td’ s when the user clicks on that particular row i.e row where in i need to pass all the values to the function which i will call

If any one knows how to do this, kindly let me know , any code is appreciated

Thanks & Regards
GG

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@AdamBrillJul 17.2003 — Try this:&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function abc(element){
children = element.childNodes;
for(x=0; x&lt;children.length; x++){
alert(children[x].innerHTML);
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;tr onClick="javascript:abc(this);"&gt;
&lt;td&gt;&lt;input type = checkbox&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=text name=txt1&gt;abc&lt;/td&gt;
&lt;td&gt;&lt;input type=text name=txt1&gt;xyz&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Let me know if you wanted it different than that... ?
Copy linkTweet thisAlerts:
@gnaneshauthorJul 17.2003 — Adam

Thanks for the code, Here what i did

var children = document.first.element[0].childNodes;

for(x=0; x<children.length; x++){

alert(children[x].innerHTML);

}

where 'first' is my form name and i will not be knowing how many row's the table will return as its get's generated from the backend , finally it will be in Table only, i will be calling a function inside <TR>, I am getting document.first.element.childNodes is null or object

Let me know if i need to change anything on this

Regards

GG
Copy linkTweet thisAlerts:
@AdamBrillJul 17.2003 — Try this instead of the other function:function abc(element){
children = element.childNodes;
for(x=0; x&lt;children.length; x++){
test = children[x].getElementsByTagName('input');
for(y=0; y&lt;test.length; y++){
alert(test[y].value);
}
}
}
Is that what you want? If not, let me know... ?
Copy linkTweet thisAlerts:
@gnaneshauthorJul 17.2003 — Adam

test = children[x].getElementsByTagName('input');

what i should pass to this method , is the table name or form name ? i.e. replace 'input'

Regards

GG
Copy linkTweet thisAlerts:
@AdamBrillJul 17.2003 — "input" shouldn't get replace by anything. It is exactly how it should be. ? Try it and see if you get the desired results. What it is doing is taking all of the <input tags and alerting the value of them all. I think that's what you wanted... ?
Copy linkTweet thisAlerts:
@gnaneshauthorJul 17.2003 — Thanks , thought so

By the way i tried with your piece of function , it says 'element' is undefined at the below line

children = element.childNodes;

even if i call like

children = document.element.childNodes;

it say's null or not an object

Any advice

Regards

GG
Copy linkTweet thisAlerts:
@AdamBrillJul 17.2003 — &lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function abc(element){
children = element.childNodes;
for(x=0; x&lt;children.length; x++){
test = children[x].getElementsByTagName('input');
for(y=0; y&lt;test.length; y++){
alert(test[y].value);
}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;tr onClick="java script:abc(this);"&gt;
&lt;td&gt;&lt;input type = checkbox&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=text name=txt1&gt;abc&lt;/td&gt;
&lt;td&gt;&lt;input type=text name=txt1&gt;xyz&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

Try this code and see if it works. You probably forgot to put the "this" inside the function when you were calling it. Also, make sure you delete the space out of the javascript:abc(this), since the forums adds that in there. ?
Copy linkTweet thisAlerts:
@gnaneshauthorJul 17.2003 — I have passed 'this' to my function, By the way i am able to get some value 'on' when i called this function, But let me know how do i access the value of each <td>'s

Regards

GG
Copy linkTweet thisAlerts:
@AdamBrillJul 17.2003 — Are you trying to get the value of the form elements or the innerHTML of the tds? The one that I just posted will get the values of the form elements...
Copy linkTweet thisAlerts:
@gnaneshauthorJul 17.2003 — I am trying to get the values inside <TD>xxx</TD> i.e i need 'xxx' from this tag's , By the way if i do

children = obj.childNodes;

children.length --- i will get 7 as i have 7 Columns , then

if i do like this

test = children[x].getElementsByTagName('td');

alert(test.length);

i passed 'td' instead of 'input' then i am getting the length '0',

Let me know how do i get the values of 'TD'

Regards

Gnanesh
Copy linkTweet thisAlerts:
@AdamBrillJul 17.2003 — This(my original post) will get whatever is inside the <td> tags:&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function abc(element){
children = element.childNodes;
for(x=0; x&lt;children.length; x++){
alert(children[x].innerHTML);
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;tr onClick="javascript:abc(this);"&gt;
&lt;td&gt;&lt;input type = checkbox&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=text name=txt1&gt;abc&lt;/td&gt;
&lt;td&gt;&lt;input type=text name=txt1&gt;xyz&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Remember to take the space out of the the javascript on this line:

<tr onClick="javascript:abc(this);">
×

Success!

Help @gnanesh 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...