/    Sign up×
Community /Pin to ProfileBookmark

please help with toggle rows

I am still learning sorry.
I nearly understand this!
I want several separate toggle buttons inside a table to toggle their own multiple rows
the script below is neat for one set of multiple rows,
how do i add more toggles- controlling their own separate rows.
would each toggle have a unique id that had to equal the tag name of the rows
is this just a line in the function -what would it be ?
sorry to be so new to this
thanks for any help

<html>
<head>
<script>
function toggle(thisname) {
tr=document.getElementsByTagName(‘tr’)
for (i=0;i<tr.length;i++){
if (tr[i].getAttribute(thisname)){
if ( tr[i].style.display==’none’ ){
tr[i].style.display = ”;
}
else {
tr[i].style.display = ‘none’;
}
}
}
}
</script>
<style type=”text/css”>
<!–
#apDiv1 {
position:absolute;
left:251px;
top:51px;
width:342px;
height:287px;
z-index:1;
}
–>
</style>
</head>
<body>

<div id=”apDiv1″>
<table width=”226″ border=”1″ cellspacing=”0″ cellpadding=”5″>
<tr>
<td width=”110″>Run main</td>
<td width=”18″>&nbsp;</td>
<td width=”60″><span onClick=”toggle(‘nameit’);”>toggle</span></td>
</tr>
<tr nameit=run>
<td>Run version2</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr nameit=run>
<td>Run version 3</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Walk main</td>
<td>&nbsp;</td>
<td>toggle2</td>
</tr>
<tr>
<td>Walk version2</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Walk version3</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</div>
</script>
</body>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 18.2009 — Try this:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function toggle(thisname) {
tr=document.getElementsByTagName('tr')
for (i=0;i&lt;tr.length;i++){
if (tr[i].getAttribute(thisname)){
if ( tr[i].style.display=='none' ){
tr[i].style.display = '';
} else {
tr[i].style.display = 'none';
}
}
}
}
&lt;/script&gt;
&lt;style type="text/css"&gt;
#apDiv1 {
position:absolute;
left:251px;
top:51px;
width:342px;
height:287px;
z-index:1;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="apDiv1"&gt;
&lt;table width="226" border="1" cellspacing="0" cellpadding="5"&gt;
&lt;tr&gt;
&lt;td width="110"&gt;Run main&lt;/td&gt;
&lt;td width="18"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td width="60"&gt;&lt;span onClick="toggle('nameit');"&gt;toggle&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit=run"&gt;
&lt;td&gt;Run version2&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit=run&gt;
&lt;td&gt;Run version 3&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Walk main&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&lt;span onClick="toggle('nameit2');"&gt;toggle2&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit2=walk&gt;
&lt;td&gt;Walk version2&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit2=walk&gt;
&lt;td&gt;Walk version3&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Use the [ code] [ /code] (without spaces) to make your script easier to read.
Copy linkTweet thisAlerts:
@faunusauthorFeb 18.2009 — how beautifully simple

I am so grateful

thanks

can you suggest the best way to have the toggles set to hidden when the page is first opened
Copy linkTweet thisAlerts:
@ai3rulesFeb 18.2009 — You could run the toggle's onload.

<body onload="toggle('nameit');toggle('nameit2');">
Copy linkTweet thisAlerts:
@JMRKERFeb 18.2009 — See if this makes sense to you:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Undefined&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function toggle(thisname) {
tr=document.getElementsByTagName('tr')
for (i=0;i&lt;tr.length;i++){
if (tr[i].getAttribute(thisname)){
if ( tr[i].style.display=='' ){
tr[i].style.display = 'none';
} else {
tr[i].style.display = '';
}
}
}
}
function init() {
var RowNames = ['nameit1','nameit2'];
tr=document.getElementsByTagName('tr')
for (i=0;i&lt;tr.length;i++){
for (j=0; j&lt;RowNames.length; j++) {
if (tr[i].getAttribute(RowNames[j])) { tr[i].style.display = 'none'; }
}
}
}
&lt;/script&gt;
&lt;style type="text/css"&gt;
#apDiv1 {
position:absolute;
left:251px;
top:51px;
width:342px;
height:287px;
z-index:1;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body onload="init()"&gt;

&lt;div id="apDiv1"&gt;
&lt;table width="226" border="1" cellspacing="0" cellpadding="5"&gt;
&lt;tr&gt;
&lt;td width="110"&gt;Run main&lt;/td&gt;
&lt;td width="18"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td width="60"&gt;&lt;span onClick="toggle('nameit1');"&gt;toggle&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit1=run"&gt;
&lt;td&gt;Run version2&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit1=run&gt;
&lt;td&gt;Run version 3&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Walk main&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&lt;span onClick="toggle('nameit2');"&gt;toggle2&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit2=walk&gt;
&lt;td&gt;Walk version2&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr nameit2=walk&gt;
&lt;td&gt;Walk version3&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Good Luck! ?
Copy linkTweet thisAlerts:
@faunusauthorFeb 18.2009 — very elegant

thanks so much

I wouldnt say it makes sense to me yet but its working well

I have to import this principal into a table with a load of other functions etc. but this has overcome a big hurdle

thanks
×

Success!

Help @faunus 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,
)...