vkv
vkv

Reputation: 1010

Href customizing from jQuery

I have an <a> tag:

<a id="down" data-Id="" type="button"  href="@Url.Action("InvestigatorDetailsDownload", "ClinicalRegistryManager")?investigatorId={data-Id}">download</a>

I am setting this attribute (data-id) from jQuery:

function showInvDetails(id) {

  $.ajax({
      url: "@Url.Action("
      method ", "
      controller ")?investigatorId=" + id

  }).done(function(data) {
      if (data) {
          console.log(id);
          $('#down').data("Id", id);
      }
  });
}

I want to set that id in the href of the a tag, how do I do that?

Upvotes: 0

Views: 54

Answers (2)

Ted
Ted

Reputation: 4067

$('#down').data('Id', id);

jQuery data() - Store arbitrary data associated with the specified element and/or return the value that was set.

data() you can imagine it as a hidden attribute associated with the anchor that jQuery creates for you and lets you get the value back whenever you need it

to change the actual id you need to use attr() like this:

$('#down').attr('Id', id);

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You need to update the href attribute as

$('#down').attr('href', "@Url.Action("InvestigatorDetailsDownload", "ClinicalRegistryManager")?investigatorId=" + id);

Also note, when using .data() jQuery uses internal cache. The statement data(key) will read default value only from data-key attribute afterwards it will use internal cache. the statement .data( key, value ) will store data in internal cache it will not update DOM.

Upvotes: 1

Related Questions