1

I am rendering a partial view CREATE as popup when create button is clicked.

This means dynamically a div is appended to main view on button click.

After this div is generated and all elements inside this are loaded, after that I want to call javascript functions.

All the answers I am able to find are using .load() event, but my scenario is different. I am not loading partial view div using ("#div").load(URL, function(){}); my div is generated dynamically

Please suggest some help

2
  • 1
    What do you mean by "generated dynamically" ? Do you create the div in JS and then append it to the body of the page ? Commented Jul 11, 2016 at 12:51
  • Just defer the code you want to run after appending / inserting the div. It'll give the DOM the time to make the div available for other code. This assumes the code you want to run affects the div. Commented Jul 11, 2016 at 13:36

1 Answer 1

2

JavaScript is executed synchronously. If you are building "the view" using jQuery and then inserting it into the DOM, then just execute your "post render" code after the markup is inserted into the DOM.

$('#myContainer').append(someMarkup);
execSomePostRenderFunction();

If you are using ajax to load the partial view/html content from the server then you would use the load() function and put your post render code in the callback.

$('#myContainer').load('route/to/Partial', execSomePostRenderFunction);

or you can use an anonymous function:

$('#myContainer').load('route/to/Partial', function(){
    //...do something here
});

Your question is a little confusing because you have the question tagged ajax but say you are not loading the partial view. You also might want to specify which MVC framework your are using so someone can give you more targeted guidance.

Partial Views in ASP.NET MVC, for example, are a completely server-side technology and the only way to render a partial view after page has loaded (like on a button click event) is through ajax and the load() function (you could also use $.get() or $.ajax() but load() is easier; that's why it was created).

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.