Send value of a <a> onclick

Asked

Viewed 1,598 times

-1

I’m trying to get the value of one <a> send it to a Javascript function, but I don’t know what’s going wrong.

//HTML
<div id="dicaDiv" class="text-center">
    <p id="pDica1" style="display: inline;">Dica 1: <span id="mostrarDica1"><a value="dica1" href="javascript:gerarDica(this.value);">mostrar dica</a></span>.</p><br>
    <p id="pDica2" style="display: inline;">Dica 2: <span id="MostrarDica2"><a value="dica2" href="javascript:gerarDica(this.value);">mostrar dica</a></span>.</p><br>
</div>

//JS
var dica;
function gerarDica(dica) {
if(dica == "dica1") {
    if(randomClasse == 0)
        dica = "veículo";
    else if(randomClasse == 1)
        dica = "animal";
    else if(randomClasse == 2)
        dica = "cor";
    document.getElementById("mostrarDica1").innerHTML = dica;
    document.getElementById("botaoDica").style.display = "none";
    }
}

The variable randomClasse is working, the problem is when I try to put in this if(dica == "dica1").

2 answers

0

Your question is a little confusing but I think that’s what you want

    //HTML
<div id="dicaDiv" class="text-center">
    <p id="pDica1" style="display: inline;">Dica 1: 
        <span id="mostrarDica1">
            <a href="#" onclick="gerarDica('dica1');">mostrar dica</a>
        </span>.
    </p>
    <br>
    <p id="pDica2" style="display: inline;">Dica 2: 
        <span id="MostrarDica2">
            <a href="#" onclick="gerarDica('dica2');">mostrar dica</a>
        </span>.
    </p>
    <br>
</div>

//JS
var dica;
function gerarDica(dica) {
    if(dica == "dica1") {
        if(randomClasse == 0)
            dica = "veículo";
        else if(randomClasse == 1)
            dica = "animal";
        else if(randomClasse == 2)
            dica = "cor";
        document.getElementById("mostrarDica1").innerHTML = dica;
        document.getElementById("botaoDica").style.display = "none";
    }
  }

I suggest using switch when you have more than two linked ifs

function gerarDica(dica) {
    if(dica == "dica1") {
        switch (randomClasse) {
          case 0:
            dica = "veículo";
            break;

          case 1:
            dica = "animal";
            break;

          case 2:
            dica = "cor";
            break;

          default:
            dica ='alguma valor padrao';
            break;
      }
        document.getElementById("mostrarDica1").innerHTML = dica;
        document.getElementById("botaoDica").style.display = "none";
    }
  }
  • still not going, https://jsfiddle.net/7y5ksugj/

  • I tested it here and it was http://ws.ri2b.com.br/validcad.com.br/testes/teste.php

0

If you want Javascript to be executed by clicking on a link, do not use href, and yes onclick.

Other details:

  • the tag a has no attribute value. Then an alternative is to pass the value directly to the function gerarDica
  • it is customary to leave the href with # and make the call function on onclick return false, so that the link is not followed.

I gave a simplified in your code so it has only the relevant parts to the solution of this specific problem, but you can adapt to your code easily:

function gerarDica(dica) {
    // vou só imprimir o valor para mostrar que está funcionando
    console.log(dica);

    // ... faz tudo que precisa fazer com a dica
  
    // retorna false para o link não ser "seguido"
    return false;
}
<a href="#" onclick="javascript:return gerarDica('dica1');">mostrar dica1</a>
<br>
<a href="#" onclick="javascript:return gerarDica('dica2');">mostrar dica2</a>

  • continue not going https://jsfiddle.net/7y5ksugj/

  • "not going" is too vague, what exactly is going on? Well, I saw that you changed the generatDica, but it is using double quotes inside double quotes, which generates error. Use single quotes, as I did above (gerarDica('dica2') instead of gerarDica("dica2")). Don’t forget the return gerarDica('dica2') to prevent links from being followed (the browser "reloads the page" if it does not have Return). And your "Tutorial" and "Play" links are also using href instead of onclick

  • And after your if(dica == "dica1") I missed opening the keys ({). And there is no element with id equal to botaoDica, therefore the document.getElementById("botaoDica") finds nothing, and trying to set the style of it, makes a mistake.

Browser other questions tagged

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