Suja Shyam
Suja Shyam

Reputation: 971

$.post issue in Jquery specific scenario

Let me share my jQuery part first

$(".btnUpdateInput").click(function() {
            //alert("Update");
            var BldDocumentId = $("#BldDocId").val();

            var BldinstructionId = $("#BldIPInstnId").val();
            var InputFieldId = $("#InputFieldId").val();
            var InputFieldValue = jQuery.trim($(this).parent().prev().text()); //$("#InputFieldValue").val();
            var InputFieldUserValue = jQuery.trim($(this).prev().val()); // $(".user_input_value").val();
            alert(BldDocumentId + "," + BldinstructionId + "," + InputFieldId + "," + InputFieldValue + "," + InputFieldUserValue);

            var postResult;
            alert($(this).get());
            $.post("/Build/UpdateInputField",
                { bldDocId: BldDocumentId, bldInstnId: BldinstructionId, inputFieldId: InputFieldId, inputFieldValue: InputFieldValue, inputFieldUserValue: InputFieldUserValue },
                function(result) {
                postResult = result;
                    //Either this should function**
                    alert($(this).get()); // returned Object[Object] but expected the clicked button
                    alert($(this).parent().get());// returned null
                    alert($(this).parent().next().get());//returned null
                    alert($(this).parent().next().children().first().get());// returned null

                    // $(this).parent().next().show();
                    // $(this).parent().next().children().first().text(InputFieldUserValue);
                    // $(this).parent().hide();
                });

                alert(postResult);
            //Or this should function**
            if (postResult == true) {
                $(this).parent().next().show();
                $(this).parent().next().children().first().text(InputFieldUserValue);
                $(this).parent().hide();
            }
        });

Now let me explain my issue. I need to show and hide few divs with respect to the button "btnUpdateInput" I clicked. I tried it in two ways: 1. I gave the following lines in the in the success part of $.post

$.post("/Build/UpdateInputField",
                { bldDocId: BldDocumentId, bldInstnId: BldinstructionId, inputFieldId: InputFieldId, inputFieldValue: InputFieldValue, inputFieldUserValue: InputFieldUserValue },
                function(result) {
                postResult = result;
                    //Either this should function**
                    alert($(this).get()); // returned Object[Object] but expected the clicked button
                    alert($(this).parent().get());// returned null
                    alert($(this).parent().next().get());//returned null
                    alert($(this).parent().next().children().first().get());// returned null

                    // $(this).parent().next().show();
                    // $(this).parent().next().children().first().text(InputFieldUserValue);
                    // $(this).parent().hide();
                });

2. or get the value of postResult out and compare and do the same there. The code is bellow:

 if (postResult == true) {
                $(this).parent().next().show();
                $(this).parent().next().children().first().text(InputFieldUserValue);
                $(this).parent().hide();
            }

Neither works for me. In 1 am not getting $(this) as the button 'btnUpdateInput' that i click and 2. the value of postResult is undefined since there is no delay so that postResult is assigned to result of post action.

Please help me out with either of the scenario.

Upvotes: 0

Views: 76

Answers (1)

Marco Johannesen
Marco Johannesen

Reputation: 13134

This is the element in your current IN. So in the SUCCES its in the return.

So in method one you could do:

$(".btnUpdateInput").click(function(e) {
var self = e;
//code
alert($(self).get()); // returned Object[Object] but expected the clicked button
alert($(self).parent().get());// returned null
alert($(self).parent().next().get());//returned null
alert($(self).parent().next().children().first().get());// returned null
}

Seems self is also used as default....

Try this instead:

$(".btnUpdateInput").click(function(e) {
var myButton = $(this);
//code
alert($(myButton).get()); // returned Object[Object] but expected the clicked button
alert($(myButton).parent().get());// returned null
alert($(myButton).parent().next().get());//returned null
alert($(myButton).parent().next().children().first().get());// returned null
}

Upvotes: 1

Related Questions