0

If I call

$('#display').text('' + data.Display);

then my div is populated with the correct value

if I call

$('#display').text(data.Display);

then it is not populated?

data is coming from a getJson call and is of the format Display: "AB01 AAAA 0000 0000 1111 2222 2233 3"

$(document).ready(function () {
    $('#getButton').click(function () {

    var id = $('#valueId').val();
    $('#Display').text('');

    $.getJSON('api/Generate/', { 'IdNumber': id })
      .done(function (data) {
          $('#Display').text('' + data.Display); // fails here unless added to ''
      })
      .fail(function (jqXHR, textStatus, err) {
          $('#error').text('Error: ' + err);
      });
    });
});

Is there a better way to put the data.Display into the correct tag, without having to add it to an empty string?

3
  • 1
    What type is data.Display (since it doesn't seem like it's a string)?
    – Kevin Ji
    Nov 20, 2013 at 8:01
  • According to VS (this is inside MVC application) it's a string already?
    – BlueChippy
    Nov 20, 2013 at 8:07
  • This was down to a a JQueryUI version issue! Updated JQueryUI and it's working as expected.
    – BlueChippy
    Nov 20, 2013 at 9:04

3 Answers 3

1

As just a blind shot in the dark, maybe try

$('#Display').text(data.Display.toString());

When you called the original function as

$('#display').text('' + data.Display);

you are coercing your data.Display to a string, so it displays correctly. Automatic coercion stops happening when you remove the string concatenation, however.

1
  • Hmm...VS is telling me this is already a string? Guess it's lying!
    – BlueChippy
    Nov 20, 2013 at 8:08
0
data.Display.toString()  ?

Correct mе if I'm wrong

0

It turns out this was down to a JQueryUI version issue. The particular version (1.8.20) was for some reason seeing {field: "string"} as an object.

Updated to 1.10.3 and now {field: "string"} is a string!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.