jquery svg paths: clicking path B check if path A is currently active, if so deactivate A and activate B












1














First of all, thanks for your interest.
I've got this SVG (country map), and I want the user to click an area of that map so that a div with information about that area pops up and that area (SVG) gets a new color('active').
If the user clicks again on that area, it will get it's previous state and div will hide. So far, so good.



The problem is, if an area is currently 'active' (e.g Area A) and the user clicks another area(e.g Area B), both Areas will be active, and I'd like Area A to deactivate.



So far this is my code:



$(function (){
$('.distrito').on('click', function(){
if ( $(this).hasClass( 'distrito' ) ) {
$(this).removeClass('distrito').addClass('distritoIsActive');
} else {
$(this).removeClass('distritoIsActive').addClass('distrito');
}
/*this will get corresponding div to showup*/
$('#legenda' + this.id).toggle(1000);
});
});


I think I should loop through paths searching for the distritoIsActive class, and I did and created an array with all areas:



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));


But I'm not able to loop through the array so the app will know if there's a distritoIsActive class and pass it to my function.
Can someone help me please?
Thanks in advance



You can find sample code here:
http://mapa.e-forma.pt/










share|improve this question
























  • Can you create a working example (something minimal) that shows the issue?
    – Dekel
    Nov 12 at 1:05










  • If only one thing can be active, use a global and store the active element in it.
    – Robert Longson
    Nov 12 at 7:25










  • Answering @Dekel: Sure I've uploaded some sample code. You can find it here: mapa.e-forma.pt Sorry I didn't do that in first place.
    – FroPT
    Nov 12 at 9:59










  • Answering @ Robert Longson: Hi, thanks for your help, but not sure if I understood that. Could you give me some example?
    – FroPT
    Nov 12 at 10:02










  • @FroPT explain what do to and what is the issue. (I click on this/click on that/expecting A/B/C). You can also add a gif-animation of the issue.
    – Dekel
    Nov 12 at 10:36
















1














First of all, thanks for your interest.
I've got this SVG (country map), and I want the user to click an area of that map so that a div with information about that area pops up and that area (SVG) gets a new color('active').
If the user clicks again on that area, it will get it's previous state and div will hide. So far, so good.



The problem is, if an area is currently 'active' (e.g Area A) and the user clicks another area(e.g Area B), both Areas will be active, and I'd like Area A to deactivate.



So far this is my code:



$(function (){
$('.distrito').on('click', function(){
if ( $(this).hasClass( 'distrito' ) ) {
$(this).removeClass('distrito').addClass('distritoIsActive');
} else {
$(this).removeClass('distritoIsActive').addClass('distrito');
}
/*this will get corresponding div to showup*/
$('#legenda' + this.id).toggle(1000);
});
});


I think I should loop through paths searching for the distritoIsActive class, and I did and created an array with all areas:



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));


But I'm not able to loop through the array so the app will know if there's a distritoIsActive class and pass it to my function.
Can someone help me please?
Thanks in advance



You can find sample code here:
http://mapa.e-forma.pt/










share|improve this question
























  • Can you create a working example (something minimal) that shows the issue?
    – Dekel
    Nov 12 at 1:05










  • If only one thing can be active, use a global and store the active element in it.
    – Robert Longson
    Nov 12 at 7:25










  • Answering @Dekel: Sure I've uploaded some sample code. You can find it here: mapa.e-forma.pt Sorry I didn't do that in first place.
    – FroPT
    Nov 12 at 9:59










  • Answering @ Robert Longson: Hi, thanks for your help, but not sure if I understood that. Could you give me some example?
    – FroPT
    Nov 12 at 10:02










  • @FroPT explain what do to and what is the issue. (I click on this/click on that/expecting A/B/C). You can also add a gif-animation of the issue.
    – Dekel
    Nov 12 at 10:36














1












1








1







First of all, thanks for your interest.
I've got this SVG (country map), and I want the user to click an area of that map so that a div with information about that area pops up and that area (SVG) gets a new color('active').
If the user clicks again on that area, it will get it's previous state and div will hide. So far, so good.



The problem is, if an area is currently 'active' (e.g Area A) and the user clicks another area(e.g Area B), both Areas will be active, and I'd like Area A to deactivate.



So far this is my code:



$(function (){
$('.distrito').on('click', function(){
if ( $(this).hasClass( 'distrito' ) ) {
$(this).removeClass('distrito').addClass('distritoIsActive');
} else {
$(this).removeClass('distritoIsActive').addClass('distrito');
}
/*this will get corresponding div to showup*/
$('#legenda' + this.id).toggle(1000);
});
});


I think I should loop through paths searching for the distritoIsActive class, and I did and created an array with all areas:



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));


But I'm not able to loop through the array so the app will know if there's a distritoIsActive class and pass it to my function.
Can someone help me please?
Thanks in advance



You can find sample code here:
http://mapa.e-forma.pt/










share|improve this question















First of all, thanks for your interest.
I've got this SVG (country map), and I want the user to click an area of that map so that a div with information about that area pops up and that area (SVG) gets a new color('active').
If the user clicks again on that area, it will get it's previous state and div will hide. So far, so good.



The problem is, if an area is currently 'active' (e.g Area A) and the user clicks another area(e.g Area B), both Areas will be active, and I'd like Area A to deactivate.



So far this is my code:



$(function (){
$('.distrito').on('click', function(){
if ( $(this).hasClass( 'distrito' ) ) {
$(this).removeClass('distrito').addClass('distritoIsActive');
} else {
$(this).removeClass('distritoIsActive').addClass('distrito');
}
/*this will get corresponding div to showup*/
$('#legenda' + this.id).toggle(1000);
});
});


I think I should loop through paths searching for the distritoIsActive class, and I did and created an array with all areas:



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));


But I'm not able to loop through the array so the app will know if there's a distritoIsActive class and pass it to my function.
Can someone help me please?
Thanks in advance



You can find sample code here:
http://mapa.e-forma.pt/







javascript jquery loops svg path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:10

























asked Nov 12 at 0:43









FroPT

207




207












  • Can you create a working example (something minimal) that shows the issue?
    – Dekel
    Nov 12 at 1:05










  • If only one thing can be active, use a global and store the active element in it.
    – Robert Longson
    Nov 12 at 7:25










  • Answering @Dekel: Sure I've uploaded some sample code. You can find it here: mapa.e-forma.pt Sorry I didn't do that in first place.
    – FroPT
    Nov 12 at 9:59










  • Answering @ Robert Longson: Hi, thanks for your help, but not sure if I understood that. Could you give me some example?
    – FroPT
    Nov 12 at 10:02










  • @FroPT explain what do to and what is the issue. (I click on this/click on that/expecting A/B/C). You can also add a gif-animation of the issue.
    – Dekel
    Nov 12 at 10:36


















  • Can you create a working example (something minimal) that shows the issue?
    – Dekel
    Nov 12 at 1:05










  • If only one thing can be active, use a global and store the active element in it.
    – Robert Longson
    Nov 12 at 7:25










  • Answering @Dekel: Sure I've uploaded some sample code. You can find it here: mapa.e-forma.pt Sorry I didn't do that in first place.
    – FroPT
    Nov 12 at 9:59










  • Answering @ Robert Longson: Hi, thanks for your help, but not sure if I understood that. Could you give me some example?
    – FroPT
    Nov 12 at 10:02










  • @FroPT explain what do to and what is the issue. (I click on this/click on that/expecting A/B/C). You can also add a gif-animation of the issue.
    – Dekel
    Nov 12 at 10:36
















Can you create a working example (something minimal) that shows the issue?
– Dekel
Nov 12 at 1:05




Can you create a working example (something minimal) that shows the issue?
– Dekel
Nov 12 at 1:05












If only one thing can be active, use a global and store the active element in it.
– Robert Longson
Nov 12 at 7:25




If only one thing can be active, use a global and store the active element in it.
– Robert Longson
Nov 12 at 7:25












Answering @Dekel: Sure I've uploaded some sample code. You can find it here: mapa.e-forma.pt Sorry I didn't do that in first place.
– FroPT
Nov 12 at 9:59




Answering @Dekel: Sure I've uploaded some sample code. You can find it here: mapa.e-forma.pt Sorry I didn't do that in first place.
– FroPT
Nov 12 at 9:59












Answering @ Robert Longson: Hi, thanks for your help, but not sure if I understood that. Could you give me some example?
– FroPT
Nov 12 at 10:02




Answering @ Robert Longson: Hi, thanks for your help, but not sure if I understood that. Could you give me some example?
– FroPT
Nov 12 at 10:02












@FroPT explain what do to and what is the issue. (I click on this/click on that/expecting A/B/C). You can also add a gif-animation of the issue.
– Dekel
Nov 12 at 10:36




@FroPT explain what do to and what is the issue. (I click on this/click on that/expecting A/B/C). You can also add a gif-animation of the issue.
– Dekel
Nov 12 at 10:36












1 Answer
1






active

oldest

votes


















0














I added a simple check in your code to get to what you wanted. Have a look below (just the js part)



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));
let previous = null;

/*let $redClassIsActive = false;*/
console.log($myDistrictos);

$(function (){
$('.distrito').on('click', function(){

if(previous === null)
{
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;
return;
}



if(previous !== null && previous.id === this.id)
{
previous = null;
$(this).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + this.id).toggle(1000);
}
else
{
$(previous).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + previous.id).toggle(1000);
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;

}


});
});


Happy coding






share|improve this answer























  • Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
    – FroPT
    Nov 12 at 22:13










  • Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
    – FroPT
    Nov 12 at 22:56












  • Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
    – Mpho Shaun Majenge
    Nov 12 at 23:23










  • Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
    – FroPT
    Nov 13 at 23:18










  • No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
    – Mpho Shaun Majenge
    Nov 14 at 0:06











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254700%2fjquery-svg-paths-clicking-path-b-check-if-path-a-is-currently-active-if-so-dea%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I added a simple check in your code to get to what you wanted. Have a look below (just the js part)



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));
let previous = null;

/*let $redClassIsActive = false;*/
console.log($myDistrictos);

$(function (){
$('.distrito').on('click', function(){

if(previous === null)
{
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;
return;
}



if(previous !== null && previous.id === this.id)
{
previous = null;
$(this).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + this.id).toggle(1000);
}
else
{
$(previous).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + previous.id).toggle(1000);
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;

}


});
});


Happy coding






share|improve this answer























  • Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
    – FroPT
    Nov 12 at 22:13










  • Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
    – FroPT
    Nov 12 at 22:56












  • Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
    – Mpho Shaun Majenge
    Nov 12 at 23:23










  • Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
    – FroPT
    Nov 13 at 23:18










  • No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
    – Mpho Shaun Majenge
    Nov 14 at 0:06
















0














I added a simple check in your code to get to what you wanted. Have a look below (just the js part)



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));
let previous = null;

/*let $redClassIsActive = false;*/
console.log($myDistrictos);

$(function (){
$('.distrito').on('click', function(){

if(previous === null)
{
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;
return;
}



if(previous !== null && previous.id === this.id)
{
previous = null;
$(this).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + this.id).toggle(1000);
}
else
{
$(previous).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + previous.id).toggle(1000);
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;

}


});
});


Happy coding






share|improve this answer























  • Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
    – FroPT
    Nov 12 at 22:13










  • Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
    – FroPT
    Nov 12 at 22:56












  • Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
    – Mpho Shaun Majenge
    Nov 12 at 23:23










  • Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
    – FroPT
    Nov 13 at 23:18










  • No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
    – Mpho Shaun Majenge
    Nov 14 at 0:06














0












0








0






I added a simple check in your code to get to what you wanted. Have a look below (just the js part)



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));
let previous = null;

/*let $redClassIsActive = false;*/
console.log($myDistrictos);

$(function (){
$('.distrito').on('click', function(){

if(previous === null)
{
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;
return;
}



if(previous !== null && previous.id === this.id)
{
previous = null;
$(this).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + this.id).toggle(1000);
}
else
{
$(previous).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + previous.id).toggle(1000);
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;

}


});
});


Happy coding






share|improve this answer














I added a simple check in your code to get to what you wanted. Have a look below (just the js part)



let $svgObject = document.getElementById('Layer_1');
let $myDistrictos = Array.from($svgObject.querySelectorAll("path"));
let previous = null;

/*let $redClassIsActive = false;*/
console.log($myDistrictos);

$(function (){
$('.distrito').on('click', function(){

if(previous === null)
{
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;
return;
}



if(previous !== null && previous.id === this.id)
{
previous = null;
$(this).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + this.id).toggle(1000);
}
else
{
$(previous).removeClass('distritoIsActive').addClass('distrito');
$('#legenda' + previous.id).toggle(1000);
$(this).removeClass('distrito').addClass('distritoIsActive');
$('#legenda' + this.id).toggle(1000);
previous = this;

}


});
});


Happy coding







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 23:22

























answered Nov 12 at 11:05









Mpho Shaun Majenge

1416




1416












  • Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
    – FroPT
    Nov 12 at 22:13










  • Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
    – FroPT
    Nov 12 at 22:56












  • Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
    – Mpho Shaun Majenge
    Nov 12 at 23:23










  • Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
    – FroPT
    Nov 13 at 23:18










  • No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
    – Mpho Shaun Majenge
    Nov 14 at 0:06


















  • Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
    – FroPT
    Nov 12 at 22:13










  • Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
    – FroPT
    Nov 12 at 22:56












  • Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
    – Mpho Shaun Majenge
    Nov 12 at 23:23










  • Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
    – FroPT
    Nov 13 at 23:18










  • No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
    – Mpho Shaun Majenge
    Nov 14 at 0:06
















Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
– FroPT
Nov 12 at 22:13




Thanks Mpho! You nailed it. I was trying to do with $redClassIsActive what you did with var previous. Some kind of flag. Thanks for your help!! :) And a big thank you to all the community!
– FroPT
Nov 12 at 22:13












Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
– FroPT
Nov 12 at 22:56






Hi Mpho, actually it's not still correct. But that's north for me. Look what happens when you select an area twice. I've uploaded your code to same url: mapa.e-forma.pt
– FroPT
Nov 12 at 22:56














Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
– Mpho Shaun Majenge
Nov 12 at 23:23




Hi FropT, sorry for the previous solution with a bug. I was still at work when I rushy did it. See the updated code sample, it should solve your problem 100%.
– Mpho Shaun Majenge
Nov 12 at 23:23












Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
– FroPT
Nov 13 at 23:18




Hi Mpho! Thank you so much for helping me. I've been working like a dog, therefore I really appreciate the time you spent on helping me. You rock as a coder and as a fellow! Big thanks bro :)
– FroPT
Nov 13 at 23:18












No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
– Mpho Shaun Majenge
Nov 14 at 0:06




No problem Bro. It was a fun task to do nonetheless. I'm glad you found a solution
– Mpho Shaun Majenge
Nov 14 at 0:06


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254700%2fjquery-svg-paths-clicking-path-b-check-if-path-a-is-currently-active-if-so-dea%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini