How to make the button go to different links? According to the variables of my "form"

Asked

Viewed 52 times

0

function myFunction() {
  var cor = document.getElementById("Cor").value;
  var tamanho = document.getElementById("Tamanho").value;
  console.log(cor)
  console.log(tamanho)
  if (cor === "Luz Fria" && tamanho === "3m X 2m") {
    document.getElementById("Botao-Compra").href = "https://luzfria.com";
  }
  if (cor === "Luz Quente" && tamanho === "3m X 2m") {
    document.getElementById("Botao-Compra").href = "https://luzquente.com";
  }

}
a:link,
a:visited {
  background-color: #f44336;
  color: white;
  padding: 14px 25px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}
<p>Escolha seu kit:</p>

<select id="Cor" onchange="myFunction()">
  <option value="Luz Fria">Luz Fria
    <option value="Luz Quente">Luz Quente

</select>

<select id="Tamanho" onchange="myFunction()">
  <option value="3m X 2m">3m X 2m


</select>

<a id="Botao-Compra" href="https://luzfria.com">Quero comprar!</a>

Good morning, I am a very lay person in the subject and I have a problem that I can not solve, I even posted a question here, but because I do not understand very well, they have just deactivated the question, so I’m trying again, this time explaining a little better, before there was no time, because they deactivated.

I need that by clicking the button (I want to buy!) it goes to a URL if the client chooses a color and to another URL if the client chooses the other color option, for example, if the client selects the cold light, it goes to http://luzfria.com if the customer selects hot light, it goes to http://luzquente.com.

Our payment platform has a link to each combination when choosing a product. If you choose the color "Warm Light" and size "3Mx2M", when you click the buy button, it goes to the hot light payment URL. If you choose the color "cold light" and size 3Mx2M, when you click the buy button, it goes to the cold light payment URL. It turns out that by clicking the buy button, it always takes me to the same link, in case the link inserted in the buy button: https://luzquente.com

I apologize if I didn’t explain myself properly, if you’re a complete layperson, I’ve searched the Internet for several days, but I haven’t.. found with jQuery, but I don’t know how to insert this my site, or how it works.

  • 1

    Hello Thayná, the other question has not been deactivated, the blue box above has the instructions on how to solve, it is not allowed to duplicate the question to get around the problem, just edit the original question and follow the tips already mentioned by the other colleague: https://answall.com/questions/478311/galera-preciso-que-meu-bot%C3%a3o-v%C3%a1-para-um-link-em-fun%C3%A7%C3%a3o-das-Vari%C3%a1veis-luz-quen#89comment6631_478311 - Thank you for understanding.

1 answer

1

Your problem is actually quite simple, just missing a key to close your if.

if (color === "Warm Light" && size == "3m X 2m") {

And you should leave as default value the cold light on the button so it works as you developed.

Botao-Compra

I do not know what kind of project is intended, if it is only for studies, if it is not, I would recommend you try to better restructure your line of thought.

  • The key was actually just a typo, I already made the change, thank you. // Even changing the pattern to cold light as above, when selecting cold light or hot light, it always sends me to the same link, in case the default cold light link on the button.

  • Did not understand, the goal would not be when it modifies the value of the select and click the button go to the selected value? I checked your modification and performed the test, seems normal to me.

  • Yes, go to cold light or warm light URL.

  • You’re not behaving that way?

  • No, for me it always goes to luzfria.

  • Here it works, when pasting in the html of my page, to insert the button, it always goes to cold light, I do not understand.

  • I created a chat here at stackoverflow, enter there maybe I can identify your problem, name: How to make the button go to different links

  • Gustavo, I couldn’t get in because I didn’t score enough. By pasting exactly the HTML above into my Wordpress, it reacts differently, always sending me to the COLD LIGHT URL

  • It will be difficult to find your problem only in this part since it is working. Your problem is probably in some other part of your code that is preventing js codes from running.

  • Gustavo Can you see in jsdiddle ? https://jsfiddle.net/#&togetherjs=Bljahv4jur

Show 5 more comments

Browser other questions tagged

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