Change div class name with onlick for 3 divs depending on which link div's link is clickedHTML5 Canvas vs. SVG vs. divget the sibling link class when the the other sibling link is clicked?Expandable div (on click), inline-block, not push down other div's on second lineChange div class onclick and backCannot display HTML stringOn Click Change Multiple Div Classes in Current Div OnlyChanging a div's style when clicked, and changing it back when another div is clickedChanging class name of a span inside a specific divChanging the background image when clicking on the div?Changing elements in div based on another div click
Is it recommended against to open-source the code of a webapp?
Java guess the number
Payment instructions from HomeAway look fishy to me
Why does Kathryn say this in 12 Monkeys?
How to pass a regex when finding a directory path in bash?
How to skip replacing first occurrence of a character in each line?
Company did not petition for visa in a timely manner. Is asking me to work from overseas, but wants me to take a paycut
Strange symbol for two functions
Select items in a list that contain criteria #2
Does the first version of Linux developed by Linus Torvalds have a GUI?
Building a road to escape Earth's gravity by making a pyramid on Antartica
Translating 'Liber'
Is it possible to (7 day) schedule sleep time of a hard drive?
Why don't B747s start takeoffs with full throttle?
Can a user sell my software (MIT license) without modification?
How can you travel on a trans-Siberian train when it is fully booked?
Why does NASA use higher frequencies even though they have worse Free Space Path Loss (FSPL)?
When writing an error prompt, should we end the sentence with a exclamation mark or a dot?
Does the growth of home value benefit from compound interest?
Turing patterns
What's the correct term for a waitress in the Middle Ages?
Traffic law UK, pedestrians
Deformation of rectangular plot
Bent spoke design wheels — feasible?
Change div class name with onlick for 3 divs depending on which link div's link is clicked
HTML5 Canvas vs. SVG vs. divget the sibling link class when the the other sibling link is clicked?Expandable div (on click), inline-block, not push down other div's on second lineChange div class onclick and backCannot display HTML stringOn Click Change Multiple Div Classes in Current Div OnlyChanging a div's style when clicked, and changing it back when another div is clickedChanging class name of a span inside a specific divChanging the background image when clicking on the div?Changing elements in div based on another div click
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have three divs each with a link inside, when a link is clicked I want the class name of all three divs to change, depending on which link is clicked.
Each div has the class name of w-33 (which makes the div 33% wide), I need the div classes to change to w-60 and w-20.
I have it so the class name changes when the link is clicked but it's only to make the first div w-60 and the other two w-20 regardless of which div link is clicked, I don't know if there's a more efficient way to make it so the div which contains the link that is clicked is the one that gets the w-60 class name other than making three separate functions for each link, so any help is appreciated.
Here are the three divs before the onclick
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
Here's the javascript
function changeclass()
var NAME = document.getElementById("first-project")
NAME.className="w-60 ml-100 mp-100 project"
var NAME = document.getElementById("second-project")
NAME.className="w-20 ml-100 mp-100 project"
var NAME = document.getElementById("third-project")
NAME.className="w-20 ml-100 mp-100 project"
Here are the three divs after the onclick
<div class="w-60" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
So I need it to be if the link in the first div is clicked, its class name will change to w-60 and the other two classes will change to w-20. if the link in the second div is clicked, the first and last class will change to w-20, and the second to w-60, if the third div's link is clicked that will be the div to get the w-60 class name.
javascript html
add a comment |
I have three divs each with a link inside, when a link is clicked I want the class name of all three divs to change, depending on which link is clicked.
Each div has the class name of w-33 (which makes the div 33% wide), I need the div classes to change to w-60 and w-20.
I have it so the class name changes when the link is clicked but it's only to make the first div w-60 and the other two w-20 regardless of which div link is clicked, I don't know if there's a more efficient way to make it so the div which contains the link that is clicked is the one that gets the w-60 class name other than making three separate functions for each link, so any help is appreciated.
Here are the three divs before the onclick
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
Here's the javascript
function changeclass()
var NAME = document.getElementById("first-project")
NAME.className="w-60 ml-100 mp-100 project"
var NAME = document.getElementById("second-project")
NAME.className="w-20 ml-100 mp-100 project"
var NAME = document.getElementById("third-project")
NAME.className="w-20 ml-100 mp-100 project"
Here are the three divs after the onclick
<div class="w-60" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
So I need it to be if the link in the first div is clicked, its class name will change to w-60 and the other two classes will change to w-20. if the link in the second div is clicked, the first and last class will change to w-20, and the second to w-60, if the third div's link is clicked that will be the div to get the w-60 class name.
javascript html
Pass index in functionchangeclass(1), changeclass(2), changeclass(3)
and check them inside funciton
– dagalti
Mar 24 at 15:32
is it ok to use jQuery?
– Doppio
Mar 24 at 15:37
add a comment |
I have three divs each with a link inside, when a link is clicked I want the class name of all three divs to change, depending on which link is clicked.
Each div has the class name of w-33 (which makes the div 33% wide), I need the div classes to change to w-60 and w-20.
I have it so the class name changes when the link is clicked but it's only to make the first div w-60 and the other two w-20 regardless of which div link is clicked, I don't know if there's a more efficient way to make it so the div which contains the link that is clicked is the one that gets the w-60 class name other than making three separate functions for each link, so any help is appreciated.
Here are the three divs before the onclick
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
Here's the javascript
function changeclass()
var NAME = document.getElementById("first-project")
NAME.className="w-60 ml-100 mp-100 project"
var NAME = document.getElementById("second-project")
NAME.className="w-20 ml-100 mp-100 project"
var NAME = document.getElementById("third-project")
NAME.className="w-20 ml-100 mp-100 project"
Here are the three divs after the onclick
<div class="w-60" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
So I need it to be if the link in the first div is clicked, its class name will change to w-60 and the other two classes will change to w-20. if the link in the second div is clicked, the first and last class will change to w-20, and the second to w-60, if the third div's link is clicked that will be the div to get the w-60 class name.
javascript html
I have three divs each with a link inside, when a link is clicked I want the class name of all three divs to change, depending on which link is clicked.
Each div has the class name of w-33 (which makes the div 33% wide), I need the div classes to change to w-60 and w-20.
I have it so the class name changes when the link is clicked but it's only to make the first div w-60 and the other two w-20 regardless of which div link is clicked, I don't know if there's a more efficient way to make it so the div which contains the link that is clicked is the one that gets the w-60 class name other than making three separate functions for each link, so any help is appreciated.
Here are the three divs before the onclick
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
Here's the javascript
function changeclass()
var NAME = document.getElementById("first-project")
NAME.className="w-60 ml-100 mp-100 project"
var NAME = document.getElementById("second-project")
NAME.className="w-20 ml-100 mp-100 project"
var NAME = document.getElementById("third-project")
NAME.className="w-20 ml-100 mp-100 project"
Here are the three divs after the onclick
<div class="w-60" id="first-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="second-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
<div class="w-20" id="third-project">
<a class="learn-more" onclick="changeclass()">Learn More</a>
</div>
So I need it to be if the link in the first div is clicked, its class name will change to w-60 and the other two classes will change to w-20. if the link in the second div is clicked, the first and last class will change to w-20, and the second to w-60, if the third div's link is clicked that will be the div to get the w-60 class name.
javascript html
javascript html
asked Mar 24 at 15:22
dulhacdulhac
487
487
Pass index in functionchangeclass(1), changeclass(2), changeclass(3)
and check them inside funciton
– dagalti
Mar 24 at 15:32
is it ok to use jQuery?
– Doppio
Mar 24 at 15:37
add a comment |
Pass index in functionchangeclass(1), changeclass(2), changeclass(3)
and check them inside funciton
– dagalti
Mar 24 at 15:32
is it ok to use jQuery?
– Doppio
Mar 24 at 15:37
Pass index in function
changeclass(1), changeclass(2), changeclass(3)
and check them inside funciton– dagalti
Mar 24 at 15:32
Pass index in function
changeclass(1), changeclass(2), changeclass(3)
and check them inside funciton– dagalti
Mar 24 at 15:32
is it ok to use jQuery?
– Doppio
Mar 24 at 15:37
is it ok to use jQuery?
– Doppio
Mar 24 at 15:37
add a comment |
6 Answers
6
active
oldest
votes
Hey you can do that with the help of parameters like this
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(1)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(2)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(3)">Learn More</a>
</div>
function changeclass(div_number)
if(div_number==1)
document.getElementById("first-project").className="w-60 ml-100 mp-100 project";
document.getElementById("second-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
if(div_number==2)
document.getElementById("second-project").className="w-60 ml-100 mp-100 project";
document.getElementById("first-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
add a comment |
You can do something like this
<div class="projects">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
</div>
<script>
function changeclass(elem)
let allDivs = document.querySelectorAll('.projects > div');
for(let oneDiv of allDivs)
oneDiv.className = "w-20";
elem.parentNode.className = "w-60";
</script>
add a comment |
Here is how I would do it. I would define css styles in classes in my css file-
for example
.selected
color: white;
background: steelblue;
and i my JS-
var resetButton = document.getElementById("reset");
now in order to apply css class to an element I would do-
resetButton.classList.add("selected");
or
now in order to remove css class to an element I would do-
resetButton.classList.remove("selected");
or simply toggle
resetButton.toggleClass("selected");
add a comment |
You can pass the event to the function, which will allow you to target the clicked element. So in your inline js it would be onclick="changeClass(event)
. Then the function would be something like this:
function changeClass(event)
// Set all elements classes to their defaults.
document.getElementById("first-project").classList = ['w-20', 'project'];
document.getElementById("second-project").classList = ['w-20', 'project'];
document.getElementById("third-project").classList = ['w-20', 'project'];
// Set new classes to the clicked element only
event.target.classList = ['w-60', 'project'];
add a comment |
You can utilize css selector "adjacent" (".hilight + div" = select all neighbours div of '.hilight')
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
add a comment |
My solution. pass event to function and add class w-60 to current element.
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325307%2fchange-div-class-name-with-onlick-for-3-divs-depending-on-which-link-divs-link%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hey you can do that with the help of parameters like this
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(1)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(2)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(3)">Learn More</a>
</div>
function changeclass(div_number)
if(div_number==1)
document.getElementById("first-project").className="w-60 ml-100 mp-100 project";
document.getElementById("second-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
if(div_number==2)
document.getElementById("second-project").className="w-60 ml-100 mp-100 project";
document.getElementById("first-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
add a comment |
Hey you can do that with the help of parameters like this
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(1)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(2)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(3)">Learn More</a>
</div>
function changeclass(div_number)
if(div_number==1)
document.getElementById("first-project").className="w-60 ml-100 mp-100 project";
document.getElementById("second-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
if(div_number==2)
document.getElementById("second-project").className="w-60 ml-100 mp-100 project";
document.getElementById("first-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
add a comment |
Hey you can do that with the help of parameters like this
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(1)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(2)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(3)">Learn More</a>
</div>
function changeclass(div_number)
if(div_number==1)
document.getElementById("first-project").className="w-60 ml-100 mp-100 project";
document.getElementById("second-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
if(div_number==2)
document.getElementById("second-project").className="w-60 ml-100 mp-100 project";
document.getElementById("first-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
Hey you can do that with the help of parameters like this
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(1)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(2)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(3)">Learn More</a>
</div>
function changeclass(div_number)
if(div_number==1)
document.getElementById("first-project").className="w-60 ml-100 mp-100 project";
document.getElementById("second-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
if(div_number==2)
document.getElementById("second-project").className="w-60 ml-100 mp-100 project";
document.getElementById("first-project").className="w-20 ml-100 mp-100 project";
document.getElementById("third-project").className="w-20 ml-100 mp-100 project";
answered Mar 24 at 15:34
Faizal HussainFaizal Hussain
299211
299211
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
add a comment |
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
That works perfect thank you! Any chance you know how to change the class names back when the link is clicked again?
– dulhac
Mar 24 at 15:39
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
Imagine situation when he has 20 such links :-)
– Alexey Zelenin
Mar 24 at 15:40
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
you mean on alternate clicks?
– Faizal Hussain
Mar 24 at 15:44
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
Alternate clicks and when the link is clicked again to reverse it
– dulhac
Mar 24 at 15:46
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
yes ,there are multiple ways to do it one method is to check the number of clicks , if the count is idd write a set of function if it is even write another
– Faizal Hussain
Mar 24 at 15:53
add a comment |
You can do something like this
<div class="projects">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
</div>
<script>
function changeclass(elem)
let allDivs = document.querySelectorAll('.projects > div');
for(let oneDiv of allDivs)
oneDiv.className = "w-20";
elem.parentNode.className = "w-60";
</script>
add a comment |
You can do something like this
<div class="projects">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
</div>
<script>
function changeclass(elem)
let allDivs = document.querySelectorAll('.projects > div');
for(let oneDiv of allDivs)
oneDiv.className = "w-20";
elem.parentNode.className = "w-60";
</script>
add a comment |
You can do something like this
<div class="projects">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
</div>
<script>
function changeclass(elem)
let allDivs = document.querySelectorAll('.projects > div');
for(let oneDiv of allDivs)
oneDiv.className = "w-20";
elem.parentNode.className = "w-60";
</script>
You can do something like this
<div class="projects">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="changeclass(this)">Learn More</a>
</div>
</div>
<script>
function changeclass(elem)
let allDivs = document.querySelectorAll('.projects > div');
for(let oneDiv of allDivs)
oneDiv.className = "w-20";
elem.parentNode.className = "w-60";
</script>
answered Mar 24 at 15:39
Alexey ZeleninAlexey Zelenin
975
975
add a comment |
add a comment |
Here is how I would do it. I would define css styles in classes in my css file-
for example
.selected
color: white;
background: steelblue;
and i my JS-
var resetButton = document.getElementById("reset");
now in order to apply css class to an element I would do-
resetButton.classList.add("selected");
or
now in order to remove css class to an element I would do-
resetButton.classList.remove("selected");
or simply toggle
resetButton.toggleClass("selected");
add a comment |
Here is how I would do it. I would define css styles in classes in my css file-
for example
.selected
color: white;
background: steelblue;
and i my JS-
var resetButton = document.getElementById("reset");
now in order to apply css class to an element I would do-
resetButton.classList.add("selected");
or
now in order to remove css class to an element I would do-
resetButton.classList.remove("selected");
or simply toggle
resetButton.toggleClass("selected");
add a comment |
Here is how I would do it. I would define css styles in classes in my css file-
for example
.selected
color: white;
background: steelblue;
and i my JS-
var resetButton = document.getElementById("reset");
now in order to apply css class to an element I would do-
resetButton.classList.add("selected");
or
now in order to remove css class to an element I would do-
resetButton.classList.remove("selected");
or simply toggle
resetButton.toggleClass("selected");
Here is how I would do it. I would define css styles in classes in my css file-
for example
.selected
color: white;
background: steelblue;
and i my JS-
var resetButton = document.getElementById("reset");
now in order to apply css class to an element I would do-
resetButton.classList.add("selected");
or
now in order to remove css class to an element I would do-
resetButton.classList.remove("selected");
or simply toggle
resetButton.toggleClass("selected");
answered Mar 24 at 15:40
SamkariaSSamkariaS
12
12
add a comment |
add a comment |
You can pass the event to the function, which will allow you to target the clicked element. So in your inline js it would be onclick="changeClass(event)
. Then the function would be something like this:
function changeClass(event)
// Set all elements classes to their defaults.
document.getElementById("first-project").classList = ['w-20', 'project'];
document.getElementById("second-project").classList = ['w-20', 'project'];
document.getElementById("third-project").classList = ['w-20', 'project'];
// Set new classes to the clicked element only
event.target.classList = ['w-60', 'project'];
add a comment |
You can pass the event to the function, which will allow you to target the clicked element. So in your inline js it would be onclick="changeClass(event)
. Then the function would be something like this:
function changeClass(event)
// Set all elements classes to their defaults.
document.getElementById("first-project").classList = ['w-20', 'project'];
document.getElementById("second-project").classList = ['w-20', 'project'];
document.getElementById("third-project").classList = ['w-20', 'project'];
// Set new classes to the clicked element only
event.target.classList = ['w-60', 'project'];
add a comment |
You can pass the event to the function, which will allow you to target the clicked element. So in your inline js it would be onclick="changeClass(event)
. Then the function would be something like this:
function changeClass(event)
// Set all elements classes to their defaults.
document.getElementById("first-project").classList = ['w-20', 'project'];
document.getElementById("second-project").classList = ['w-20', 'project'];
document.getElementById("third-project").classList = ['w-20', 'project'];
// Set new classes to the clicked element only
event.target.classList = ['w-60', 'project'];
You can pass the event to the function, which will allow you to target the clicked element. So in your inline js it would be onclick="changeClass(event)
. Then the function would be something like this:
function changeClass(event)
// Set all elements classes to their defaults.
document.getElementById("first-project").classList = ['w-20', 'project'];
document.getElementById("second-project").classList = ['w-20', 'project'];
document.getElementById("third-project").classList = ['w-20', 'project'];
// Set new classes to the clicked element only
event.target.classList = ['w-60', 'project'];
edited Mar 24 at 15:44
answered Mar 24 at 15:38
Marc RandallMarc Randall
662
662
add a comment |
add a comment |
You can utilize css selector "adjacent" (".hilight + div" = select all neighbours div of '.hilight')
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
add a comment |
You can utilize css selector "adjacent" (".hilight + div" = select all neighbours div of '.hilight')
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
add a comment |
You can utilize css selector "adjacent" (".hilight + div" = select all neighbours div of '.hilight')
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
You can utilize css selector "adjacent" (".hilight + div" = select all neighbours div of '.hilight')
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
function hilight (hilightID)
var allDivs = document.getElementsByClassName('w-33');
for (i = 0; i < allDivs.length; i++)
const theDiv = allDivs[i];
const isHilight = theDiv.id === hilightID;
if (isHilight)
theDiv.classList.add("hilight");
else
theDiv.classList.remove("hilight");
.container display: flex;
.container > div background: salmon;
.w-33 width: 33%;
.hilight width: 60%;
.hilight + div width: 20%;
<div class="container">
<div class="w-33" id="first-project">
<a class="learn-more" onclick="hilight('first-project')">Learn More</a>
</div>
<div class="w-33" id="second-project">
<a class="learn-more" onclick="hilight('second-project')">Learn More</a>
</div>
<div class="w-33" id="third-project">
<a class="learn-more" onclick="hilight('third-project')">Learn More</a>
</div>
</div>
answered Mar 24 at 16:01
DoppioDoppio
952611
952611
add a comment |
add a comment |
My solution. pass event to function and add class w-60 to current element.
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
add a comment |
My solution. pass event to function and add class w-60 to current element.
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
add a comment |
My solution. pass event to function and add class w-60 to current element.
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
My solution. pass event to function and add class w-60 to current element.
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
function changeClass(event)
document.querySelectorAll('.project').forEach(el =>
el.classList.remove('w-60');
el.classList.add('w-20')
)
let cls = event.target.parentNode.classList
cls.remove('w-20');
cls.add('w-60');
#group
font-size: 26px;
margin: 0px;
display: flex;
.w-60,
.w-20,
.w-33
display: inline-flex;
justify-content: center;
align-items: center;
padding: 15px;
border: 4px solid red;
transition:all .5s;
.w-33
width: 33%;
.w-60
width: 60%;
background: beige;
.w-20
width: 19.4%;
background: lightpink;
<div id="group">
<div class="w-33 project" id="first-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="second-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
<div class="w-33 project" id="third-project">
<a class="learn-more" onclick="changeClass(event)">Learn More</a>
</div>
</div>
answered Mar 24 at 16:29
dagaltidagalti
762137
762137
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325307%2fchange-div-class-name-with-onlick-for-3-divs-depending-on-which-link-divs-link%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Pass index in function
changeclass(1), changeclass(2), changeclass(3)
and check them inside funciton– dagalti
Mar 24 at 15:32
is it ok to use jQuery?
– Doppio
Mar 24 at 15:37