Javascript does not work on Safari

Asked

Viewed 187 times

0

My code is working in Chrome, but in Safari it doesn’t go through any if else and goes straight to the else.

//FUNÇÃO PARA ATIVAR NA SELEÇÃO DOS DROPBOX
var selecao= document.querySelectorAll('.validation-lenient'); 
function handleClick (event)
{

//IDENTIFICAÇÃO DO CAMPO SOMA PELO ID DO FORMULARIO HTML
var soma = document.getElementById('campo-soma').innerText;

//PREENCIMENTO DO CAMPO SOMA
var camposoma = document.querySelector('[name="field46[]"]').value = soma;

//VARIAVEL IF PARA PREÇO COM BASE NAS SOMAS DOS PESOS ANTERIORES
if (camposoma == "2 2 50 Almoço") {
      nomefesta = "Festa VIP";
      nomefesta2 = "Festa TOP";
      tabelaprazo = "TABELA | PRAZO(10%) | AVISTA (15%)";
      precoVip = " valor | valor | valor";
      precoTop = " valor | valor | valor";
  }
else if (camposoma == "2 2 60 Almoço") {
      nomefesta = "Festa VIP";
      nomefesta2 = "Festa TOP";
      tabelaprazo = "TABELA | PRAZO(10%) | AVISTA (15%)";
      precoVip = " valor | valor | valor";
      precoTop = " valor | valor | valor";
  }
else {
    precoVip = "não se aplica";
    precoTop = "não se aplica";
}

//PREENCIMENTO DO CAMPO ORÇAMENTO
var campoorcamento = document.querySelector('[name="field51[]"]').value = precoVip;
var campoorcamento = document.querySelector('[name="field52[]"]').value = precoTop;
var campoorcamento = document.querySelector('[name="field58[]"]').value = nomefesta;
var campoorcamento = document.querySelector('[name="field63[]"]').value = nomefesta2;

}
//FIM DA FUNÇÃO DOS DOS DROPBOX
selecao.forEach(function(item){
item.addEventListener('click', handleClick, {once: false});
});

1 answer

2


It shouldn’t be working even in Chrome (or any other browser), because you’re comparing an element to a string, see:

if (camposoma == "2 2 50 Almoço") {

The variable camposoma was previously declared as an object:

var camposoma = document.querySelector('[name="field46[]"]').value = soma;

See that this variable references the element document.querySelector('[name="field46[]"]'), not at its value, although it is doing two things at the same time: declaring the variable and changing the value of the element.

Another possible compatibility problem is in the use of .innerText. Exchange for .textContent. You may also need to use the .trim() to remove possible spaces before and after the string. It would:

var soma = document.getElementById('campo-soma').textContent.trim();

Regarding the if’s, put .value after camposoma to return the value of the element:

if (camposoma.value == "2 2 50 Almoço") {
      nomefesta = "Festa VIP";
      nomefesta2 = "Festa TOP";
      tabelaprazo = "TABELA | PRAZO(10%) | AVISTA (15%)";
      precoVip = " valor | valor | valor";
      precoTop = " valor | valor | valor";
  }
else if (camposoma.value == "2 2 60 Almoço") {
      nomefesta = "Festa VIP";
      nomefesta2 = "Festa TOP";
      tabelaprazo = "TABELA | PRAZO(10%) | AVISTA (15%)";
      precoVip = " valor | valor | valor";
      precoTop = " valor | valor | valor";
  }
else {
    precoVip = "não se aplica";
    precoTop = "não se aplica";
}
  • I believe that is not the problem, after all, it defines the value of soma for document.getElementById('campo-soma').innerText, however, I think the problem is to get the value with innerText, there is a post with this same problem.

  • The problem is in the variable camposoma, and not on the variable soma.

  • Yes, but she’s assigned with var camposoma = document.querySelector('[name="field46[]"]').value = soma;, first it assigns the variable value soma to the element he has just sought by querySelector, and after that, assign in the variable camposoma, at the end, the value of camposoma is equal to that of the soma.

  • I understood, but I didn’t see anything abnormal in it. The only problem I noticed in the code was what I described in the answer.

  • 1

    Ah tah. Actually innerText may not be compatible with Safari. I’ll add in the reply. Thanks!

Browser other questions tagged

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