How to add and remove classes when clicking an SVG path?

Asked

Viewed 2,117 times

2

I am working with a map of Brazil in SVG, and I need that when you click on a state, it add the "active" class and remove the "active" class from the others. It also has the function of adding the state name to an input.

Example:

<path id="Santa Catarina" class="BR-SC RegiaoSul str1" d="M101205 160059l0 -33 -32 -32 32" />

<script type="text/javascript">
    $(docutment).ready(function() {
        $(".str1").click(function(){
            var state = $(this).attr("id");
            $(".str1").removeClass("active");
            $(this).addClass("active");
            $("input[name=estado]").val( state );
        });
    });
</script>

With jQuery not working of course, I would like to do this in javascript, someone can help me?

  • Is it an open source svg library from the map? What are you using? If you can post an example on jsFiddle

  • I only have the SVG file, the script not yet, because I don’t know how to do it. Here’s the link http://pastebin.com/NVPUnEub

1 answer

1

An example: here

Later, I edit and explain better how it works, now I have little time.

The more look there if it was like this?

Any doubt comments aew. Hug.

EDIT 1

This is the remove code and adds the class:

$('.str1').click(function(ev) {
$('.str1').each(function(){
    var clazz = this.getAttribute("class");
    clazz = clazz.replace("active", "");
    this.setAttribute("class", clazz);
});
ev.target.setAttribute("class", "active " + ev.target.getAttribute("class"));
});

EDIT 2

I updated the previous code to also get the id and add it to input. Follows updated code:

$('.str1').click(function(ev) {
$('.str1').each(function(){
    var clazz = this.getAttribute("class");
    clazz = clazz.replace("active", "");
    this.setAttribute("class", clazz);
});
ev.target.setAttribute("class", "active " + ev.target.getAttribute("class"));        
var estado = ev.target.getAttribute("id"); // sem jquery
/* ou */
//var estado = $(ev.target).attr('id'); // com jquery
$('#estado').val(estado);
});

I updated the example also.

  • Yes, on the add and remove class part that’s right, it helped me a lot already, now if it’s not too much to ask, as it would be to click and send the id to an input, example: http://pastebin.com/8grM16Ji Thanks friend.

  • @Feera I updated the answer, looking at your initial question I did the 'input' on the map. Anything comments.

  • This is for a representative form, then the person clicks on her state and adds the state in the input, that’s right, thank you very much man, I appreciate that, strong hug for you.

  • @Feera, I don’t know if it’s open, but this svg you made or found somewhere? It is that later I will need something similar, only that not only in Brazil, but throughout Latin America.

  • I found it on the internet, on the site http://commons.wikimedia.org But I looked for an example for you, see if this helps you: http://commons.wikimedia.org/wiki/File:Southamerica_blue.svg Hug

  • http://commons.wikimedia.org/wiki/File:Blankmap-Americas.svg? uselang=en UPDATED

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.