0

I'm trying to execute two functions but it only execute the first one. I think that when I change a HTML variable value, the javascript stops its execution.

This is the code that I'm executing: HTML:

<div class="funcion" onclick="country_function();setGeo(1)">

JavaScript:

function country_function(){        
        region_hide();
        class_hide();
        country_show();
    };

function country_show(){
        var popup = document.getElementById("countrypopmenu");
        popup.classList.toggle("show");
    };
    function country_hide(){
        var popup = document.getElementById("countrypopmenu");
        popup.classList.toggle("hidden");
    };
    function region_show(){
        var popup = document.getElementById("regionpopmenu");
        popup.classList.toggle("show");
    };
    function region_hide(){
        var popup = document.getElementById("regionpopmenu");
        popup.classList.toggle("hidden");
    };
    function class_show(){
        var popup = document.getElementById("classpopmenu");
        popup.classList.toggle("show");
    };
    function class_hide(){
        var popup = document.getElementById("classpopmenu");
        popup.classList.toggle("hidden");
    };

    function setGeo(geoVal) {
      document.getElementByID('geodata').value= geoVal;
      window.alert(geoVal);
    };

Does anyone know why it doesn't execute all the functions?

3 Answers3

2

There is a typo in function satGeo :

 document.getElementById('geodata').value= geoVal;

function country_function(){        
        console.log("Inside function 1");
    };

  function setGeo(geoVal) {
    console.log("inside function 2");
  };
<div class="funcionff" onclick="country_function();setGeo(1);">click</div>
amrender singh
  • 7,949
  • 3
  • 22
  • 28
1

In setGeo, you've mis-capitalized .getElementById().

But, beyond that, you have a ton of code that is not-needed and a solution that is brittle and won't scale as your needs change.

You really just need one function that takes two arguments - what element to show/hide/toggle and what operation to perform on it (show/hide/toggle).

Additionally, the practice of setting up event handlers via HTML event attributes is a 25+ year old technique that dates back to before there were web standards and there are many reasons not to use this ancient technique today. Separate your JavaScript from your HTML completely and use modern standards for event configurations.

// Get your DOM references 
var f = document.querySelector(".function");
var c = document.querySelector(".country");
var r = document.querySelector(".region");
var l = document.querySelector(".locale");

// Set up your event handlers in JavaScript, not HTML
f.addEventListener("click", function(){
  // Just call the single function as many times as needed
  // with the appropriate arguments each time:
  showHideToggle(c,"show");
  showHideToggle(r,"hide");
  showHideToggle(l,"toggle");
});

// One function to do all operations
function showHideToggle(element, operation){
 // For show/hide/toggle operations just do the right
 // thing with the "hidden" class. 
 switch (operation.toLowerCase()) {
   case "show":
     element.classList.remove("hidden");
     break;
   case "hide":
     element.classList.add("hidden");
     break;
   default:
     element.classList.toggle("hidden");
     break;      
 }
}
div { opacity:1; transition:all 1s;  }
.function { cursor:pointer; user-select:none; margin:1em; }
.hidden { opacity:0; }
<div class="function">Click Me</div>
<div class="country hidden">Country</div>
<div class="region">Region</div>
<div class="locale hidden">Locale</div>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
-1

I suggest to only attach one function to onclick attribute, then execute as many functions as needed in that event handler.

For example:

<div class="function" onclick="onFunctionButtonClick()"></div>

function onFunctionButtonClick () {
  region_hide();
  class_hide();
  country_show();
  setGeo(1);
}

Or even put the listener logic in JavaScript, so you can keep your HTML away from main application logic and have cleaner HTML.

<div class="function"></div>

var functionButton = document.getElementsByClassName('function')[0];
functionButton.addEventListener('click', onFunctionButtonClick);
kit
  • 535
  • 4
  • 10
  • `document.getElementsByClassName()` scans the entire document for all matching nodes, so it doesn't make sense to immediately throw out all but the first one after doing that. Additionally, it returns a "live" node list, which impedes performance. `.querySelector()` is what you want. – Scott Marcus Jun 12 '18 at 15:54