0

I am new to both Django and Javascript and am having trouble calling a user defined function from within a javascript function in Django template.

I have a javascript block as follows:

function EditDialog(pk) {       
   $.ajax({
       url: "{% url 'populatereviewform' %}",
       method: 'GET',
       data: {
       pk: pk
       },
       success: function(formHtml){
          //Do something
          alert("Success!")
       },
       dataType: 'html'
    });

    // this is my function that I would like to call
    function MyFunc(e, offset) {
       alert("Calling my function")
    }

    // Now I try to call this function from another function
    ("#dialog").submit(function(e)
    {
        this.MyFunc(e, "1");
        return false;
    });
}

My question is how can I call this nested function? In the code above, I am creating a function called MyFunc which I am trying to call from the submit method. However, this comes back with this.MyFunc is not a function.

1 Answer 1

1

The this is bound to something else.

Remove this and call your function like this: MyFunc().

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry should have mentioned it, but that does not work. I get the same error without the this.
You also need to add the $ on $("#dialog")

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.