Use value of a function

-1

I want to use the value inside a function outside of it. I have the global variable, and since it is a variable that is never the same, it presents it. I tested this, and only ran once with the undifined value, because the function was not executed.

var circle_x;

function deselectElement(evt){
  if(selectedElement != 0){
    selectedElement.parentNode.removeAttributeNS(null, "onmousemove");
    selectedElement.removeAttributeNS(null, "onmouseup");
    selectedElement = 0;
    dx=evt.clientX;
    //objet 1
    if(selectedLineX == 1){

        circle_x=currentX;

    }

  }

}

alert(circle_x);
    
asked by anonymous 20.11.2014 / 16:43

1 answer

3

Delete the global variable. It probably did not work because it is extremely difficult to administer global variables. See this answer .

function deselectElement(evt){
    if(selectedElement != 0){
        selectedElement.parentNode.removeAttributeNS(null, "onmousemove");
        selectedElement.removeAttributeNS(null, "onmouseup");
        selectedElement = 0;
        dx = evt.clientX;
        //objet 1
        if(selectedLineX == 1){
            circle_x = currentX;
            return circle_x;
        }
    }
    return null; //não me parece coisa boa mas tem que retornar algo, pelo menos assim está explícito    
}

var circle_x = deselectElement(evt);
alert(circle_x);

I placed GitHub for future reference .

Here the code makes the communication as it should, through the input (parameters) and output (return) of the function.

Note that you continue to have global variables. Where does currentX come from? Even the best programmers can not manage a code full of global variables.

And the dx variable is not being used at all. The code has some weird things, but I think the central question is answered. If not, give more detail in your question.

    
20.11.2014 / 16:54