0

In my Ajax success function I'm trying to dynamically check a radiobutton.

success: function (response) {
    var equipmentInfo = JSON.parse(response.d);
    $('#<%=' + equipmentInfo.InventoryType + '.ClientID%>').prop("Checked",true);
}

but I can't figure out how to escape the second line so that it works.

karel
  • 5,489
  • 46
  • 45
  • 50
user3140169
  • 221
  • 3
  • 12
  • 1
    `equipmentInfo.InventoryType` is a Javascript variable which only exists on the client side, therefore you cannot use it in the server-side ASP `Response.Write`. – Rory McCrossan Oct 03 '19 at 15:45
  • The block inside the `<% ... %>` is .NET code. It gets parsed and executed by the server when the page is being created. It runs on the server, before the JavaScript. its job is to inject a value into the JavaScript, the final version of which is then sent to the browser. It cannot work the other way round - JavaScript cannot inject a value into that, because JS executes later, on the browser, in a totally separate context. What will happen in your code above is that the server will try to execute the literal string `equipmentInfo.InventoryType + '.ClientID` which clearly will not be valid. – ADyson Oct 03 '19 at 15:51
  • You need to find another way to associate the radio button with the inventoryType value, e.g. by adding the type as a (unique) class or data-attribute on the element, so you can use a different selector to locate it. – ADyson Oct 03 '19 at 15:54
  • Thanks for reminding about what is actually happening. I realize that this is a duplicate of sorts but the terms in my search didn't bring up anything obvious. – user3140169 Oct 03 '19 at 16:36

0 Answers0