1

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!

2 Answers 2

2

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;
       });
     });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Bellash! It worked with a mousedown() event. Thank you.
0

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')

4 Comments

I think $(".tag-buttons").attr('data-id') will return a set of matched elements consisting of all elements with class of tag-buttons. Unless there is only one element with the class of tag-buttons.
That's true. I was testing this on a separate document, but I want to use the data-id of the clicked element not all the data-id of the document.
Have you tried @Bellash answer? I think his answer will give you what you want.
When I tested it load the same page, I tried to open the link on a new tab, the old tab load the result I want with the data-id and the new one loads the same page without it. I solved this with a mousedown() event. It worked perfectly. Thank you.

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.