0

I want to edit the returned data from the $.get. I'm stuck on how to apply the newly created inputname, the problem I believe is with $(data).

id = '12';
$.get('/page.php?id='+id, function(data) {

        // split input name by hyphen
        m = $(data).find('input').attr('name').split('-');

        // build new input name attribute 
        inputname = m[0] + '-' + id + '-' + m[2] + '-' + m[3] + '-' + m[4] + '-' + m[5];

        // apply new input name to data ??? this part I'm stuck on.
        $(data).find('input').attr('name', inputname);

});
1
  • What is data? What it should be and are you sure you are getting what you want? Log it somwhere. Commented Oct 30, 2013 at 8:48

1 Answer 1

1

Try

id = '12';
$.get('/page.php?id=' + id, function (data) {
    var $data = $(data);

    // split input name by hyphen
    $data.find('input').attr('name', function (idx, name) {
        var m = name.split('-');
        var inputname = m[0] + '-' + id + '-' + m[2] + '-' + m[3] + '-' + m[4] + '-' + m[5];
        return inputname;
    });

    //be careful that the contents of `data` still will not have the updated name, but the contents of the jQuery wrapper $data will have the updated name
});
Sign up to request clarification or add additional context in comments.

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.