I have a HTML table with multiple columns and a switch. I show the information from a database in table. The table has a switch. When user clicks on the switch I want to pass the UserId to AJAX function and run a script, but I'm not able to retrieve the value in javascript function.
Here is part of the HTML Razor code in question:
@model IEnumerable<WebApplication8.Models.ManageUserViewModel>
@foreach (var item in Model)
{
string UserId = @Html.DisplayFor(model => item.UserId).ToString();
Session["UserId"] = UserId;
<tr>
<td style="display:none" id="UserId">@UserId</td>
<td align="center">
<div class="btn-group btn-toggle">
@if (item.ActiveUser)
{
<button class="btn btn-sm btn-success active" >ON</button>
<button class="btn btn-sm btn" onclick="test()">OFF</button>
}
else
{
<button class="btn btn-sm btn" onclick="On()">ON</button>
<button class="btn btn-sm btn-success active">OFF</button>
}
</div>
</td>
</tr>
}
Here is mini-test function where I'm trying to get the value of UserId:
function test(){
var UserId = document.getElementById('UserId');
alert ('UserId');
}
Any suggestion/workaround would be hugely appreciated.