I have an anchor with a class name. When I click the anchor I want the class to change. When I click it again, I want it to change back. Simple, but I don't know JS. Thanks
shockeye
0
Newbie Poster
Recommended Answers
Jump to PostYou'd need something along the lines of this
function changeClass(elem, className1,className2) { elem.className = (elem.className == className1)?className2:className1; }
Then the anchor would look like this
<a href='#' class='class1' onclick='changeClass(this,"class1","class2");'>Hello World</a>
Jump to Postwindow.onload=addLinkEvt; function addLinkEvt(){ var link = document.getElementById('changeclass'); if (link.addEventListener) { link.addEventListener('click',onLinkEvt, false); } } function onLinkEvt(e){ var className1 = 'c1'; var className2 = 'c2'; elem.className = (elem.className == className1)?className2:className1; }
All 6 Replies
ShawnCplus
456
Code Monkey
Team Colleague
AdrianaTX
0
Newbie Poster
shockeye
0
Newbie Poster
AdrianaTX
0
Newbie Poster
shockeye
0
Newbie Poster
codejoust
8
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.