1

I have the following ajax success function. How can I act on the result with jquery before appending it? The following code is my attempt, however, the second console statement never writes.

See the current .filter line. I base that as my latest attempt after reading and trying the different methods from here: Use Jquery Selectors on $.AJAX loaded HTML?

$(function ()
{
    $('#myactionlink').click(function (result)
    {
        $.ajax(this.href, {
            success: function (result)
            {
                console.log((new Date()).getTime() + ' - ' + 'result returned.');
                $(result).filter('input[name="RowId"]').each(function (i)
                {
                    var x = (i + 1).toString();
                    console.log((new Date()).getTime() + ' - ' + 'RowId: ' + x);
                    $(this).val(x);
                });
                $('#mypartial').append(result);
            }
        });
        return false;
    });
});

Html Partial being returned.

<div>
    <input id="RowId" name="RowId" type="text" value="">
</div>

Current Inprogress Solution

$(function ()
{
    $('#myactionlink').click(function (result)
    {
        $.ajax(this.href, {
            success: function (result)
            {
                console.log((new Date()).getTime() + ' - ' + 'result returned.');
                var outResult = $(result);
                outResult.filter('[name="RowId"]').each(function (i)
                {
                    var x = (i + 1).toString();
                    console.log((new Date()).getTime() + ' - ' + 'RowId: ' + x);
                    $(this).val(x);
                }).end().appendTo('#mypartial');
            }
        });
        return false;
    });
});
Community
  • 1
  • 1
Valamas
  • 24,169
  • 25
  • 107
  • 177
  • How sure are you that the result has an input element named "RowId"?Can you show us an example of what result is? – Andrew Shepherd Mar 01 '12 at 22:11
  • Because when I use $('input[name="RowId"]') instead of $(result).filter, it successfully applies the rowid. However, it does this for everyrow i have appended, not just the one I am appending, thanks. I realise I will need to have to change the function from a for each to getting the max number + 1. I am focusing on the filter part for now. The example is illogical right now. – Valamas Mar 01 '12 at 22:18
  • I am adding a sample html now.... – Valamas Mar 01 '12 at 22:24

1 Answers1

1

Store a reference to result, modify it, then append it.

var outResult = $(result);
outResult.find('[name="RowId"]').each(...).end().appendTo("#mypartial");

You were actually appending the original html and not doing anything with the one you created using $(result).

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • Kevin, I am unsure if I have implemented your suggestion successfully. i have updated my question with the latest code at the bottom. – Valamas Mar 01 '12 at 22:21
  • At this point i think the problem may lie with what is being returned by result. What does result contain? Are the inputs children of a root node in result, or are you returning only inputs? – Kevin B Mar 01 '12 at 22:24
  • Replace `.filter` with `.find`, updating example. – Kevin B Mar 01 '12 at 22:32