Uncaught ReferenceError: function is not defined

1

When I call a function, it gives:

  

Uncaught ReferenceError: function is not defined

JS:

function __startGame (__nome, __pronom) {
var canvasGame = document.getElementById("__startGame");
var secondEtap = document.getElementById("scndEtapGame");
var confirmChoiceInt = confirm("Você tem certeza que deseja escolher " + __nome + " como " + __pronom + " presidenciável?");
if (confirmChoiceInt) {
    canvasGame.style.display = "none";
    secondEtap.style.display = "block";
    function showMonLvl(a, b) {
        var spanLvl = document.getElementById("uiLvlSpan");
        var spanMon = document.getElementById("uiMonSpan");
        spanLvl.innerHTML = a;
        spanMon.innerHTML = b;
    }
    function addValue(a, b, c) {
        var spanValueEleitor = document.getElementById("eleitorValueMenuContent");
        var spanValueJuiz = document.getElementById("juizValueMenuContent");
        var spanValueJorn = document.getElementById("jornValueMenuContent");
        spanValueEleitor.innerHTML = a;
        spanValueJuiz.innerHTML = b;
        spanValueJorn.innerHTML = c;
    }
    function addValueXp(a, b, c, d, e, f) {

    }
    function changep() {
        var p1 = document.getElementById("u_06");
        p1.style.display = "none";
    }
    showMonLvl(1, "$50");
    addValue("$100", "$25 000", "$15 000");
} else {}
}

HTML:

<img class="img_seta" id="imgSetaDireita" src="imgs/setaDireita_png.png" onclick="changep();"/>

Why will it be?

    
asked by anonymous 25.10.2015 / 04:43

2 answers

1

When you call a function in this way onclick="changep();" , that is: when you call a function directly in HTML; it has to be in the global scope. Being in the global scope means that the function must be outside of other functions, or defined directly in window for example like this:

window.changep = function() {
    var p1 = document.getElementById("u_06");
    p1.style.display = "none";
}

In this case, this function does not use any of the other __startGame functions, so you can even define the function outside that function.

You have another alternative which is to run this function, or at least what it does inside an event sink. In this case you can know when the element is clicked and you run the code you need.

So instead of onclick="changep();" you can do this:

var sDireita = document.getElementById('imgSetaDireita');
sDireita.addEventListener('click', function() {
    var p1 = document.getElementById("u_06");
    p1.style.display = "none";
});

or even if the function is in global scope:

sDireita.addEventListener('click', changep);
    
25.10.2015 / 07:11
0

Ariel, your problem is the scope, the message you are issuing is that the function you are calling is undefined, when trying to access a function / method of the function __startGame() called changep() .

You can add an additional parameter to the __startGame() function to execute only a certain task.

Example:

var el = document.getElementById('meuElemtno'); // pega o elemnto button



el.addEventListener('click', function() { // no evento click 
  a('c'); // executa a função a() passando o tipo do metodo que é c();
}, false);


function a(tipo) {
  // encontra a o tipo e executa a função; 
  if (tipo === 'b') {
    b();
  } else if (tipo === 'c') {
    c();
  }

  function b() {
    console.info('acessando metodo b');
  }

  function c() {
    console.info('acessando metodo c');
  }
}
<button type="button" id="meuElemtno">Click me</button>
    
25.10.2015 / 05:54