2

I have an input element that I am setting the value to be nothing initially:

<input id="fooValue" type="hidden" data-url="@Url.Action(MVC.fooController.fooValue())" value="" />

I then have an AJAX call where I retrieve a value and try to pass it into the input and set the previously blank value to this new value:

$.ajax({
    type: 'GET',
    url: $('#fooValue').data('url'),
    success: function (data) {
        $('#fooValue').val(data);
    }
});

When I perform this AJAX request in the Firebug console I get back the value of 4, however when I got to get the value from the input it isn't returning anything. Can anybody help me and tell me why it would appear that the value is not being set with the AJAX request? Thanks.

5
  • 1
    The following post might be helpful. Although his example involves a HIDDEN field I think you may be able to fix the problem by removing value="" from your HTML as described in the blog post. Commented Apr 5, 2013 at 18:20
  • That worked actually. Thank you. I now need to get the value into my value= correctly as right now I have value="[object Object] :) thanks. Commented Apr 5, 2013 at 18:36
  • What type of data are you returning? You might want to try .val(data.d); Commented Apr 5, 2013 at 19:26
  • I reposted my comment as an Answer. I'd be grateful if you would mark it as Accepted. Commented Apr 5, 2013 at 19:43
  • If you always return a data.d, probably best to use a converter or dataFilter: api.jquery.com/jQuery.ajax Commented Apr 5, 2013 at 19:48

2 Answers 2

1

The following post might be helpful. Although his example involves a HIDDEN field I think you may be able to fix your problem by removing value="" from your HTML as described in the blog post.

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

Comments

0

Ajax is an asynchronous request. may be you try with async: false in your ajax function?

$.ajax({
  type: 'GET',
  url: $('#fooValue').data('url'),
  async: false,
  success: function (data) {
    $('#fooValue').val(data);
  }
});

Comments

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.