1

I have calendar constructor function and two prototype functions in first prototype function I am creating div element and creating it's attribute ondragstart and I want to call another prototype function on in it's second argument var that = this; that.drag(event), but when it renders it shows <div ondragstart="that.drag(event)"> console gives error that is not function. How can I call prototype function in this situation? please explain with example, thanks.

 calendar.prototype.tbl = function(){
 var that = this;
 var evDiv = document.createElement('div');     
 evDiv.setAttribute('draggable', 'true');
 evDiv.setAttribute('ondragstart', 'that.drag(event)');
 }

 calendar.prototype.drag = function(ev){
    ev.dataTransfer.setData("text", ev.target.id);
 }
2
  • 2
    Do not use setAttribute. Do not use event listener attributes. Do not use code inside strings. Commented Nov 25, 2016 at 14:10
  • Do not use event handlers (prefixed with on). Use event listeners. Commented Nov 25, 2016 at 14:43

1 Answer 1

1

Use addEventListener:

calendar.prototype.tbl = function() {
    var that = this;
    var evDiv = document.createElement('div');

    evDiv.draggable = true;
    evDiv.addEventListener('dragstart', that.drag);
};

calendar.prototype.drag = function(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.