ComputerSurgeon
ComputerSurgeon

Reputation: 11

jquery $("#id").load() not triggering document.ready() for second time

I am using jquery in asp.net MVC3 on Razor view engine. I have jquery tab control and on tab navigation i load myusercontrol.cshtml pages. On myusercontrol.cshtml page i have

 $(document).ready(function (){ loadjqgrid();});. 

On click of each tab i do this

 $("#fragment-1").load("MyAction_in_controller"); 

this is called only for the first time when i click the tab, on second click control is not passed to the controller. Instead nothing happens. Please advice.

Thanks a lot in advance.

Upvotes: 0

Views: 1385

Answers (2)

Jack
Jack

Reputation: 8941

If I'm understanding you correctly you want to run loadjqgrid each time a tab is clicked after the load event? If so you can do:

$("#fragment-1").load("MyAction_in_controller",loadjqgrid);

Upvotes: 0

Kevin B
Kevin B

Reputation: 95027

$(document).ready only gets triggers on DOMContentLoaded event which can only happen once. Loading in new content cannot trigger that event.

Instead, abstract the code behind it into a function and call that function.

function domready () {
    // do stuff
}

$(document).ready(domready);

$("#fragment-1").load("MyAction_in_controller",domready);

However, there is more than likely a better way to handle it such as delegated events.

Upvotes: 3

Related Questions