1

I was wondering if there was a way to set the value of an input (returned via an ajax call) before appending it to the document.

 $(function () {
    $("#addItem").click(function () {
        var numRows = $('.row').length;
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                // find input id in html that contains Order ?
                // set value of the input to numRows + 1 ?

                // append updated html to an element called #rows
                $("#rows").append(html);
            }
        });
        return false;
    });

Thanks for any help

Here's what the html might look like

<div class="row">
<input type="hidden" name="OfficialAddresses.index" autocomplete="off" value="1cffe209-44e1-44ca-a013-a808bff8a174">
        <fieldset>
            <legend>Address</legend>  
            <div class="editor-field">
                <input class="text-box single-line" id="OfficialOfficialAddresses_1cffe209-44e1-44ca-a013-a808bff8a174__Order" name="OfficialOfficialAddresses[1cffe209-44e1-44ca-a013-a808bff8a174].Order" type="text" value="0">

            </div>
            <div class="clear">&amp;nsbp;</div>
            <a href="#" class="deleteRow">delete</a>
            <div class="clear">&amp;nsbp;</div>
        </fieldset>
</div>
3
  • Give us a sample of the returned html so we can give you an answer. Commented Jun 15, 2011 at 15:41
  • 1
    BTW, if the ID always ends with Order you can use the jQuery attribute-ends-width-selector. $html.find('input[id$=Order]') Commented Jun 15, 2011 at 16:00
  • Thanks matt! I didn't know that could be done. Commented Jun 15, 2011 at 16:26

4 Answers 4

3

Assuming the input's ID is Order:

success: function (html) {
    var $html = $(html);

    $html
        .find('#Order')        // find input id in html that contains Order
        .val(numRows + 1);     // set value of the input to numRows + 1

     // append updated html to an element called #rows
    $("#rows").append($html);
}

If you really mean that the input's ID only contains order, use a fancier selector:

$html.find('input[id*=Order]').val(numRows + 1);
Sign up to request clarification or add additional context in comments.

Comments

1

yes there is. If you put the returned content into a jQuery object, then you can perform selections on elements contained within and set the value.

e.g.

  $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {

                var htmlToAppend = $(html);

                // do your searching and value setting here

                // append updated html to an elemen called #rows
                $("#rows").append(html);
            }
        });

Comments

1
$(function () {
    $("#addItem").click(function () {
        var numRows = $('.row').length;
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                $('#Order', html).val(numRows+1);

                // append updated html to an elemen called #rows
                $("#rows").append(html);
            }
        });
        return false;
    });
});

Comments

0

whell, a workaround could be to insert the new html in a hidden div, then find the "order ?" input and set the value to wathever you want

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.