Home > OS >  How to use Onclick in a MVC table and still get jquery to process the click event too
How to use Onclick in a MVC table and still get jquery to process the click event too

Time:02-11

I have a table with an OnClick that works great.

<tr onclick="location.href = '@Url.Action("Index", new { id = item.CaseNumber, dp = item.DocumentPath, dt = item.DocumentType, sc = Model.SearchCriteria, rds = Model.ReturnDataSel })'">

But I want to show a spinner while it gets the data from the Url.Action. so I have jquery:

$("#res_table").click(function () {
            var x = document.getElementById("spin_id");
x.style.display = "block";

How do I get both to function at the same time? I see I can use a .ajax function to do a get call, but I dont know how to reference the data in the html i.e.(id,dp,dt,sc,rds)

CodePudding user response:

Why not merge them into one event?

<tr data-url="@(Url.Action("Index", new { id = item.CaseNumber, dp = item.DocumentPath, dt = item.DocumentType, sc = Model.SearchCriteria, rds = Model.ReturnDataSel }))">

Then:

$("tr").click(function () {
              
    //Gets the URL from the data attribute 
    var url = $(this).data("url");
    
    //Gets the spinner element
    var x = document.getElementById("spin_id");
    
    //Shows spinner element
    x.style.display = "block";
    
    //Redirects to URL
    window.location.href = url ;

});
  • Related