0

I have a dynamically generated grid with x number of textboxes that will be in it. As each textbox is generated, I give it an OnChange event that is a set function.

Html.TextBox(... new { @onchange = "ChangeItemQuantity(" + vm.ID + ", " + fk.id + ")" ...

So when it's rendered, it looks like this:

<input ... type="text" onchange="ChangeItemQuantity(1939, 3)" />

Then, in the script section:

function ChangeItemQuantity(ItemId, ForeignKeyId) {
    ...
}

In the ChangeItemQuantity() function, how would I also capture the new value of the textbox? I don't really want to use an id on the textbox, because it is part of a grid with many textboxes.

Should I pass it in as a parameter? If so, what would the syntax be of the code that renders the textbox?

Or, is there a way to capture is inside the javascript function?

Thanks!

2
  • 1
    @Html.TextBox(.... new { @class="abc" data-id="...", data-fk="..." }) and $('.abc').change(function() { var value = $(this).val(); var id = $(this).data('id'); var fk = $(this).data('fk'); ..... Commented Nov 21, 2016 at 22:35
  • Recommend you also read Unobtrusive JavaScript Commented Nov 21, 2016 at 23:00

1 Answer 1

1

If you want to store data in the html element why not use data- attributes?

Set them like so

@Html.TextBox(.... new { @class="someClass" data-vmId="vm.ID", data-fkId="fk.id" })

Then set a listener on that class

$('.someClass').change(function() { 
     var value = $(this).val(); 
     var vmid = $(this).data('vmid');
     var fkid = $(this).data('fkid');
}
Sign up to request clarification or add additional context in comments.

2 Comments

I posted a new question with more details: stackoverflow.com/questions/40734094/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.