0

I tried to set the button's link from the querySelector, but the value doesn't change, how do I get it set that way?

The idea is to change the button's link when I select the value in a dropdown box so I can click and open the link.

My attempt:

select_1
    .on("change", function(d) {
    var value_1 = d3.select(this).property("value");
    document.querySelector('#botao-de-jogo-betfair-1').href = value_1;
    });

The button:

<button class="button" style="width: 100%;" id="botao-de-jogo-betfair-1" href="link selector" target="_blank">Jogo Betfair 1</button>

When I use it in the way with <a I'll put it below, the value changes but it gets really weird with the link value inside the button, I'd like to just leave the button:

<button class="button" style="width: 100%;" ><a id="botao-de-jogo-betfair-1" href="link selector" target="_blank">Jogo Betfair 1</a></button>
Digital Farmer
  • 1,705
  • 5
  • 17
  • 67

1 Answers1

2

<button> doesn't have a href property, so you'll need change it's href attribute:

select_1
    .on("change", function(d) {
    var value_1 = d3.select(this).property("value");
    document.querySelector('#botao-de-jogo-betfair-1').setAttribute("href", value_1);
    });
vanowm
  • 9,466
  • 2
  • 21
  • 37
  • Hello friend @Vanowm , now it's really set ```href```, but when I click the ```button``` it doesn't open to a new tab and opens an error page, it doesn't open the link. Can you help me? – Digital Farmer Aug 29 '21 at 16:45
  • 1
    Perhaps this might help: https://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link – vanowm Aug 29 '21 at 16:47
  • I need to wait 6 minutes to define your answer as a solution, as soon as possible I will register, thanks! – Digital Farmer Aug 29 '21 at 16:48