Naila Akbar
Naila Akbar

Reputation: 3358

Update input tag value on anchor tag click

I've a div

@{int index = 0;}
@for (int i = 0; i < Model.lstFiles.Count; i++)
{
     <div id="attachmentDetail(@index)" name="attachmentDetail(@index)" class="panel-body">
         <a asp-action="Download" asp-controller="Admin" asp-route-realName=Model.lstFiles[i].RealName>@Model.lstFiles[i].FileName</a>
         <a onclick="btnDelFile_Click(@index)" id="btnDelFile{@index}" class="pull-right" title="delete"><img height="20" width="20" src="@Url.Content("~/images/delete.jpg")" /></a>
         <input type="text" asp-for="@Model.lstFiles[i].IsDeleted" hidden />
         <input type="text" asp-for="@Model.lstFiles[i].FileId" hidden />
         <hr />
     </div>
     index++;
 }

I want to update @Model.lstFiles[i].IsDeleted value on btnDelFile_Click. But I'm unable to get div and its child. I tried this code

$('#attachmentDetail(' + index +') :input lstFiles['+index+'].IsDeleted').val("True");

but it says

jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: #attachmentDetail(0)

Is there any better way to get child of div?? I want to update IsDeleted value, and then hide this div.

Any kind of help will be appreciated.

Upvotes: 0

Views: 673

Answers (2)

James
James

Reputation: 22237

Yes, there is a better way to get the child (and also the parent). I would set a unique class on all the clickable <a> tags which trigger your code so that you can assign the event handlers by class.

$(".uniqueClass").click(function (e) {
  var $div = $(this).parent();
  var $deletedField = $div.find("input").first();
  $deletedField.val(true);
});

Now you can get rid of all those ID attributes.

Upvotes: 1

orabis
orabis

Reputation: 2809

You can set an id for your element, and use document.getElementById to get it, and then update it accordingly.

Upvotes: 1

Related Questions