/    Sign up×
Community /Pin to ProfileBookmark

How to onClick change all classes with a certain name?

I’ve searched everywhere and can’t find it. I can do it if I ID the span, using getElementById() but I don’t want to ID it.

Is there a way to change all the <span class=”highlight”> to <span class=””> without writing an ID in the SPAN tag?

Thanks!

[CODE]<html>
<head>
<style type=”text/css”>
<!—
.highlight {
BACKGROUND-COLOR: #ffffcc;
}
—>
</style>
</head>
<body>

<a href=”#” onclick=”highlightOn()”>Turn Highlight ON</a><br />
<a href=”#” onclick=”highlightOff()”>Turn Highlight OFF</a><br />

<p>
There is some text. I want to <span class=”highlight”>highlight</span> certain <span class=”highlight”>words</span>. But I want to be able to turn off and on the highlight, based on the class name “highlight”.
</p>

</body>
</html>

[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@upand_at_themNov 16.2007 — Loop through the .getElementsByTagName() array, check the class, and change where necessary.
Copy linkTweet thisAlerts:
@potterd64Nov 16.2007 — Just remember that elem.setAttribute("class","") will not work, you have to use elem.className = "";
Copy linkTweet thisAlerts:
@benwebauthorNov 16.2007 — This will do two things: It changes the "highlight" style for any <span> tag AND it will change the text for On to Off, and vice versa:

[CODE]<html>
<HEAD>
<style type="text/css">
.none {
BACKGROUND-COLOR: #ffffff;
}
.highlight {
BACKGROUND-COLOR: #ffffcc;
}
.xxx {
BACKGROUND-COLOR: #e3e3e3;
}
</style>
<script language="JavaScript" type="text/JavaScript">
<!-- Begin
//Changes the highlight to none AND changes the On text to Off, vice versa
function hilite(fieldObj) {
var myText = fieldObj.innerHTML;
var myElement = document.getElementsByTagName("span");
var num = myElement.length;
for (var i = 0; i < num; i++) {
if (myElement[i].className == "none") {
if (fieldObj.innerHTML == 'On') {
fieldObj.innerHTML = "Off";
}
myElement[i].className = "highlight";

} else if (myElement[i].className == "highlight") {
if (fieldObj.innerHTML == 'Off') {
fieldObj.innerHTML = "On";
}
myElement[i].className = "none";
}
}
} //function hilite(fieldObj)
</script>
</HEAD>
<body>

<b>Highlight: </b><a href="#" onclick="hilite(this);">Off</a><br />

<p>
There <span class="xxx"> is some text that I don't want to highlight</span>.
I want to <span class="highlight">highlight</span>
certain <span class="highlight">words</span>.
But I want to be able to turn off and on the highlight,
based on the class name "highlight".
</p>

</body>
</html>[/CODE]
×

Success!

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