I have an ASP.NET project that is effectively a single page application, it utilises both razor and Javascript to render the page and provide user control.
Within my application there are a number of task structures that each have their own independent ID, these tasks are presented to the user within a list. As part of providing user control there are a number of buttons for each task that allow the user to do certain things; such as completing a task.
Since there are multiple tasks on a single page there needs to be a way of identifying one task from another, this is done through the taskId, which is mentioned before. For example a "Complete" button will contain a custom attribute data-taskId
, which can be then retrieved and passed on to a Javascript function for use.
However my issue is with actually providing the Javascript function with the taskId
in the first place. I am using jQuery, which provides on click functionality through .on("click")
. This means that I might have a function something like the below.
$(".complete-button").on("click",
function (event) {
//1. get taskId
//2. complete the task with taskId
});
Therefore all of my complete buttons that have the class .complete-button
will be able to execute this function. Each task has its own complete button and I want the button to be able to complete only the task it is associated with.
I need to know what is the best way to provide this function with the taskId
that is needs to operate properly. Some of the other code within this project written by another programmer makes use of hidden <input>
elements that store data within the html page, this data can then be retrieved by jQuery and used within a function. Below is an example of what this would look like:
HTML
<input type="hidden" class="case-status" value="@Model.Status" />
JS
$(".status-button").on("click",
function (event) {
var status = $(".case-status").val();
//2. do something with status
});
However this not work, as there is no way to set one of these variables beforehand due to several tasks being presented to the user at once. Since there are several tasks, there can't be one single element holding the current task, since the user could click any of the complete buttons on any of the tasks.
Instead I need a way to retrieve the data-taskId
value from the complete button and push it through to the complete button's onClick function, I'm not sure how to do this and also adhere with good practice.