Diego M.
Diego M.

Reputation: 55

How to add an HTML attribute to the current URL with jQuery

Sorry guys, noob jQuery question. This is what I have:

HTML:

<div>
    <a class="tag-buttons" href="#" data-id="&product_tag=273" data-element_type="widget" data-widget_type="button.default">
        Sistemas de asistencia
    </a>
</div>

jQuery:

 $(document).ready(function(){
    url = window.location.href + attr('data-id');
      $(".tag-buttons").click(function(){
        $(".tag-buttons").attr("href", url);
      });
    });

What I'm doing wrong?

Thank you so much!

Upvotes: 1

Views: 836

Answers (2)

Bellash
Bellash

Reputation: 8184

Here's the solution

 $(document).ready(function(){
     var url = window.location.href;
       $(".tag-buttons").click(function(){
         url+=$(this).data('id');
         // $(this).attr("href", url);
          window.location = url;
       });
     });

Upvotes: 2

moxy
moxy

Reputation: 324

When you set your url, you forgot to tell where to take the data-id attribute.

Just replace:

attr('data-id')

by

$(".tag-buttons").attr('data-id')

Upvotes: 0

Related Questions