onedevteam.com onedevteam.com - 5 months ago 30
jQuery Question

Changing asp.net core TagHelpers with Javascript

I have

<a id="continue-link" asp-controller="Account" asp-action="Register" asp-route-id="1">Continue </a>


in my asp.net core application, that generate this html when compiled:

<a href="/Account/Register/1" id="continue-link">Continue </a>


How can i change
asp-route-id
value from javascript? I tried with
$().attr
but it's not recognized.

Answer

You have to change your href attribute in generated html.

You can achive this by geting your href attribute, split it into array, then change value in array and join it again into one string with separator and then replace href attribute in your a element.

Code example:

var $link = $('#continue-link');
var href = $link.attr('href').split('/');
href[3] = 4; //here you set your new asp-route-id value
$link.attr('href', href.join('/'));

Check this codepen to see how it work.