0

i have a Problem my Area Shape href="/kosmetikstudios/deutschland/Bayern" tag look so. And i will to use the Parameter "Bayern" (this is the last parameter of the url).

I need this dynamic.

Here are my Javascript Code:

<script type="text/javascript">
  $(document).ready(function(){
  var mName="passiv";
    $("#map area").mouseenter(function(){

      mName = $(this).attr("href"); // i don't have idea to implement this
      bName="images/" +mName + ".png";
      $("#map img").attr("src",bName);

    });

    $("#map area").mouseleave(function(){
      bName="images/Europa.png";
      $("#map img").attr("src",bName);

    });

  });
</script>

Here are my HTML Code

 <area shape="poly" coords="356,300,355,299,354,298....." href="/kosmetikstudios/deutschland/Bayern" title="Kosmetikstudios in Bayern" alt="Kosmetikstudios in Bayern" />

<area shape="poly" coords="156,200,425,699,154,298....." href="/kosmetikstudios/deutschland/Berlin" title="Kosmetikstudios in Berlin" alt="Kosmetikstudios in Bayern" />

I hope anybody can help me.

Regards,

Dave

3 Answers 3

1
mName = this.href.substring(
    this.href.lastIndexOf('/') + 1
);
Sign up to request clarification or add additional context in comments.

1 Comment

You can simply use this.href, the jQuery objects are not needed.
1

Instead of extracting the value out of the URL you could set a data attribute to store the name.

For example:

HTML

<area shape="poly" data-name="Bayern" href="/kosmetikstudios/deutschland/Bayern" />
<area shape="poly" data-name="Berlin" href="/kosmetikstudios/deutschland/Berlin" />

JS

<script type="text/javascript">
  $(document).ready(function(){
  var mName="passiv";
    $("#map area").mouseenter(function(){

      //read the data attributes
      mName = $(this).data("name");

      bName="images/" +mName + ".png";
      $("#map img").attr("src", bName);

    });

    $("#map area").mouseleave(function(){
      bName="images/Europa.png";
      $("#map img").attr("src",bName);

    });

  });
</script>

1 Comment

Cool. Be awesome if you could upvote or accept it as answer if it helped. thx!
0

You can do this:

mName = $(this).attr("href").split("/").reverse()[0];

2 Comments

Consider this.href.split("/").reverse()[0];
Yes ofc, native JS is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.