Skip to content
Advertisement

How can I add action to just created object in JavaScript?

I have some strange problem with action adding to dynamically created objects. The thing is, when I am adding it, together with assigning action, the action is done automatically, not by pressing mouse button. Now, after some changes based on previous functions what I’ve achieved is totally not working action.

    optionsList = {
    Elements: new Array(),
    El: '',
    initialMouseX: undefined,
    initialMouseY: undefined,
    startX: undefined,
    startY: undefined,
    dXKeys: undefined,
    dYKeys: undefined,
    zeroX: undefined,
    zeroY: undefined,
    draggedObject: undefined,
    initListElement: function (element,type) 
    {
        if (!this.Elements[element.id])
        {
            this.Elements[element.id] = new Array();
            console.log("added new element with id: " + element.id);
        }
        if (typeof element == 'string')//jeśli element jest stringiem
        {
            element = document.getElementById(element);
        }
        
        this.Elements[element.id].type = type;
        addEventSimple(this.Elements[element.id],'mousedown',optionsList.showControls);
        console.log("element type: " + this.Elements[element.id].type + " id: " + element.id);
        return false;
    },
    
    showListControls: function(e)
    {
        var evt = e || window.event;
        optionList.showControls(this,evt);
        return false;
    },
    
    /*******************************************/
    /*The function*/
    showControls: function(obj,e)
    {
        console.log("Item pressed id is: " + obj.id);
        return false;
    }
}

Because whole source code is quite long I’ve posted it in here: http://jsfiddle.net/sebap123/smu3E/0/

The error (or non working function) is at the bottom of JS code. What I want to achieve is when the white text is pressed some action happens, based on type of object, or id.

After reading this source code many times I am still unable to find anything where mistake is.

Advertisement

Answer

since you tagged , you could use .on().

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement