/    Sign up×
Community /Pin to ProfileBookmark

Checkboxes using onclick

I have this script where, when you click the checkbox, the text next to it is highlighted.

I need it to do that for a whole list of checkboxes. (When you click checkbox 1, text 1 highlights, when you click checkbox 2, text 2 highlights, etc.) But I can’t get the syntax right. Here is the code:

[code]
<script type=”text/javascript”>

function highlight3(myform, Class) {

//Create Array of All HTML Tags
var allHTMLTags = document.getElementsByTagName(“*”);

//Loop through all tags using a for loop
for (i=0; i < allHTMLTags.length; i++) {

//Get all tags with the specified class name.
if (allHTMLTags[i].className == Class) {

if(myform.mycheckbox1.checked == true) {
allHTMLTags[i].style.backgroundColor = ‘#C39’;
}
else {
allHTMLTags[i].style.backgroundColor = ‘transparent’;
}
}
}
}

</script>

<form name=”myform”>

<input type=”checkbox” name=”mycheckbox1″ value=”value” onclick=”highlight3(this.form, ‘highlight’)”>
<span class=”highlight”> Highlight this text by checking the box </span>
[/code]

Here it is in action: [url]http://littleduck.com/test2.html[/url]

So I want to have more checkboxes named mycheckbox2, mycheckbox3, etc. How can I add code in the script to cycle through all the checkboxes?

Thank you.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJan 21.2010 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function HighLight(obj,id){
var h=document.getElementById(id);
if (h){
h.style.backgroundColor=obj.checked?'#FF0000':'transparent';
}
}
/*]]>*/
</script></head>

<body>
<input type="checkbox" name="mycheckbox1" value="value" onclick="HighLight(this, 'highlight1')">
<span id="highlight1"> Highlight this text by checking the box </span>
<input type="checkbox" name="mycheckbox1" value="value" onclick="HighLight(this, 'highlight2')">
<span id="highlight2"> Highlight this text by checking the box </span>
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@swoffordauthorJan 21.2010 — It works! Thank you very much.
×

Success!

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