Jump to content

Recommended Posts

Posted

Boas!

Tenho esta função:

function onchangeFunction(obj)
{
 var aux= new String(obj.getAttribute("id"));
 switch(aux)
 {
 case "A":
	 document.getElementById("tarefa").innerHTML = "Funciona!";
	 break;
 default:
	 document.getElementById("tarefa").innerHTML = aux;//obj.getAttribute("id");
 }
}

Quando o id do objeto é "A" deveria escrever Funciona, quando nao é escreve o id desse objecto. No entanto o id é "A" e aparece escrito o "A" em vez de funciona. Podem me dizer o que estou a fazer mal sff

Obrigado

Posted

Se te escreve A é porque não entra no primeiro case.

Verifica se a variável aux não tem espaços. A função trim, faz isso que é retira-os que é um espetáculo 😉

De qualquer das formas, não vejo razão para teres um switch. Podes fazê-lo perfeitamente com um if.

Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro.

ndsotware.org

Posted

versão um pouco mais elegante:

function onchangeFunction(obj) {
var aux = new String(obj.getAttribute("id"));
switch (aux) {
case "A":
	res = "Funciona!";
	break;
default:
	res = new String(obj.getAttribute("id"));
}
document.getElementById("tarefa").innerHTML = res;
}

Na minha opinião o switch deve reduzir-se apenas à parte que muda, i.e. ao fator decisivo.

Assim evitas copy/pastes e simplificas o código.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.