How do i click on an svg shape and it changes colour | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do i click on an svg shape and it changes colour

2nd Jul 2018, 4:17 PM
Dennis
14 Answers
+ 3
try to use the javascript onclick event
2nd Jul 2018, 4:48 PM
Roel
Roel - avatar
+ 3
Use class or id in shape tag like <rect id="colorchange" ....... > then do hover effect on class simple. But hear you should use fill:green; in hover effect in place of background-color.
2nd Jul 2018, 5:01 PM
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN - avatar
+ 3
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN that could work, but since Dennis wants the event to occur when you click, and not when you go with your mouse over the svg, its better to use onclick
2nd Jul 2018, 5:03 PM
Roel
Roel - avatar
+ 3
Roel he can use active in place of hover by giving the link # in href.
2nd Jul 2018, 5:05 PM
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN - avatar
+ 3
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN thats true, unless he wants a javascript function to occur
2nd Jul 2018, 5:06 PM
Roel
Roel - avatar
+ 3
Roel yah js is good but it need some more skills 😊
2nd Jul 2018, 5:07 PM
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN - avatar
2nd Jul 2018, 5:09 PM
Roel
Roel - avatar
+ 2
use the css :active since you only want to change its color something like this #idname:active { /*codeblock for css*/ }
2nd Jul 2018, 5:49 PM
Roel
Roel - avatar
+ 1
Or may use visited in place of active.
2nd Jul 2018, 5:58 PM
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN
BALOOCH ANZARAHMEDKHAN ZAKIRAHMEDKHAN - avatar
+ 1
but visited (i guess) stays true if clicked once
2nd Jul 2018, 5:59 PM
Roel
Roel - avatar
+ 1
ok good luck
2nd Jul 2018, 6:00 PM
Roel
Roel - avatar
0
conclusion?!js or basic svg
2nd Jul 2018, 5:31 PM
Dennis
0
let me try...
2nd Jul 2018, 6:00 PM
Dennis
0
// using jQuery // replace #shape with whatever id your shape has // You can also add/remove elements from the colors array var colors = ["red","green","blue"]; var i = 0; $(function() { $("#shape").click(function() { i++; if (i == colors.length) i = 0; $("#shape").css("background-color", colors[i]); }); });
2nd Jul 2018, 6:11 PM
ReimarPB
ReimarPB - avatar