Search code examples
javascriptdom-events

Syntax fault with JavaScript


document.getElementById("but").onclick = function(e) {
showDropDown(this, e);
};

function showDropDown(element, e) {
element.onclick = function() {};
if (e.stopPropagation) e.stopPropagation(); // W3C model
else e.cancelBubble = true; // IE model
document.getElementById("window").style.display = "inline-block";
document.onclick = function(e) {
    var ele = document.elementFromPoint(e.clientX, e.clientY);
    if (ele == element) {
        hideDropDown();
        return;
    }
    do {
        if (ele == document.getElementById("window")) return;
    } while (ele = ele.parentNode);
    hideDropDown(element);
};
}

function hideDropDown(element) {
document.onclick = function() {};
document.getElementById("window").style.display = "none";
element.onclick = function(e) {
    showDropDown(this, e);
};
}​


   <input id="but" type="button" value="pressMe" />
   <div id="window" style="display:none">popup</div>​

errors: https://www.dropbox.com/s/uzeiq6043rvueqf/Capture.PNG https://www.dropbox.com/s/w3rct18cumwva7m/bar3.png


Solution

  • What you did was where you copied or wrote this code, there most likely was a bug in it. JSFiddle has been known to have such problems. What you have to do is type that section of code (1 line above the error and 1 line underneath it) in a simple editor such as Notepad or TextEdit, and then copy that over and replace your current code. I know its this error, as the Unexoected token ILLEGAL part of it meant that the hidden character which is placed there is obviously not JavaScript-compliant, therefore it is not a syntax error at all.

    Works for me.