0

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?

3
  • you mean change the id of the anchor? Commented Oct 19, 2016 at 6:28
  • 1
    href need to pass an id, which is being set in jquery. Commented Oct 19, 2016 at 6:42
  • @Vispriya - I understand that you might be new to this but your question makes no sense. Please try and describe the problem in plain english. Also, please explain what id(data-id) means to you. Commented Oct 19, 2016 at 6:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Am I wrong about that not being a proper string value? To me that second string param screams please escape me, I understand I could be totally wrong. What syntax is that? Does @Url.Action("InvestigatorDetailsDownload", "ClinicalRegistryManager") get replaced by some sort of preprocessor?
@DaveThomas, Yes Url.Action() is asp.net-mvc preprocessor.
this is complete speculation in such an unclear question. you should try and answer the actual question, or ask for clarification
@DaveThomas, why are you apologising? my comment was directed at the answer
@DaveThomas, No need to apologize, once your edit was rejected, You are not involved in this at all.
0
$('#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);

4 Comments

data() will create a hidden attribute? No. jQuery uses internal cache to store arbitrary data associated with element.
you're right, it won't. but how do you explain Store arbitrary data associated with the specified element and/or return the value that was set. to someone who asks a question like that? data-id usualy denotes an attribute. in any case, whoever wants more details, the link is there
@Satpal - you're preaching to the choire. the link for data() is already in my answer. you just missed it, again
I have issue with the term hidden attribute, You can use more acceptable term internal cache

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.