Search code examples
javascripthtmldrag-and-drop

Drag & drop in HTML


Hi there I'm writing a little calculator for exercise. There is no logic yet. Still I wanted to allow the user to drag the calculator around the browser.

So I nested my Calculator elements in this -Tag and implemented some Drag&Drop-logic (which is mainly from a tutorial). Still it doesn't work.

Does anybody see what I do wrong?

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


                    <title>myCalculator</title>

                    <script type="text/javascript">

                    var dragObject = null;
                    var dragX = 0;
                    var dragY = 0;

                    var posX = 0;
                    var posY = 0;

                    function handleInput(currentInput){

                        alert(currentInput);

                        var x = 0;                              

                        x = currentInput;

                    }

                    function saveOperator(currentOperator){

                        alert(currentOperator);

                    }

                    function dragInit(){

                        document.onmousemove = drag;
                        document.onmouseup = dragStop;

                    }

                    function dragStop(){
                        dragObject = null;
                    }

                    function dragStart(element){
                        dragObject = element;
                        dragX = posX - dragObject.offsetLeft;
                        dragY = posY - dragObject.offsetTop;
                    }

                    function drag(event){

                        posX = document.all ? window.event.clientX : event.pageX;
                        posY = document.all ? window.event.clientY : event.pageY;

                        if(dragObject != nul){
                            dragObject.style.left = (posX - dragX) + "px";
                            dragObject.style.top = (posY - dragY) + "px";
                        }
                    }


                    </script>

    </head>
            <body onload="dragInit">




                     <div id="calc" onmousedown="dragStart(this)" style="position:absolute;top:10px;left:10px;height:270px;width:200px;background:#006600"> 


                     <h1 id="headLine"><b>myCalculator</b></h1>


                    <input type="text" id="calcField"/>


                    <table style="position: absolute;" id="calculatorTable">

                    <tr>
                        <td><input id="7" type="button" value="7" onclick="handleInput(this.value)"/></td>
                        <td><input id="8" type="button" value="8" onclick="handleInput(this.value)"/></td>
                        <td><input id="9" type="button" value="9" onclick="handleInput(this.value)"/></td>
                        <td><input id="/" type="button" value="/" onclick="saveOperator(this.value)"/></td>
                    </tr>
                    <tr>
                        <td><input id="4" type="button" value="4" onclick="handleInput(this.value)"/></td>
                        <td><input id="5" type="button" value="5" onclick="handleInput(this.value)"/></td>
                        <td><input id="6" type="button" value="6" onclick="handleInput(this.value)"/></td>
                        <td><input id="*" type="button" value="*" onclick="saveOperator(this.value)"/></td>
                    </tr>
                    <tr>
                        <td><input id="1" type="button" value="4" onclick="handleInput(this.value)"/></td>
                        <td><input id="2" type="button" value="5" onclick="handleInput(this.value)"/></td>
                        <td><input id="3" type="button" value="6" onclick="handleInput(this.value)"/></td>
                        <td><input id="-" type="button" value="-" onclick="saveOperator(this.value)"/></td>
                    </tr>
                    <tr>
                        <td><input id="0" type="button" value="0" onclick="handleInput(this.value)"/></td>
                        <td><input id="," type="button" value="," onclick="handleInput(this.value)"/></td>
                        <td><input id="+" type="button" value="+" onclick="saveOperator(this.value)"/></td>
                        <td><input id="=" type="button" value="=" onclick="saveOperator(this.value)"/></td>
                    </tr>

                    </table>
                    </div>


            </body>     
</html>

Solution

  • You have a syntax error on line 56. Use null instead of nul. You probably want to use a JS syntax checker such as JSLint.

    Then, line 66, you probably want to call dragInit function, so you should write <body onload="dragInit()"> with parentheses (not braces, thanks Lightness Races in Orbit ;)