1

I have a button and user's name on the template and if a user clicks on button, Ajax should replace user's name and button with some messages using new CSS styling.

This is my Ajax :

$('#fulfillButton').bind('click',function() {
    $.ajax({
        type : "GET",
        contentType : "application/json; charset=utf-8",
        url : "order/${orderID}",
        dateType : "json",
        cache: false,
        success: function (data) {
            if(data.statusCode=="OK")
            {
                $('#replace').replaceWith("<div class ="error_message">"+ data.body +"</div>");
            }
        },

        error: function (data) {
            alert("Something went wrong, please retry again");
        },
   });
});

This is my html:

<div id="replace">
    <div class="full_name">
        ${name}
    </div>
    <button id="fulfillButton" type="button" class="action-button shadow animate green">
        FulFill
    </button>
</div>
<button id="goBackButton" type="button" class="go_back_button">
    Go Back
</button>

However, when I clicked on the button, nothing happened. I am missing something here? I am using Jquery 1.6.

3
  • any error in debugger? Commented Jul 27, 2016 at 10:03
  • @JaxTeller If I remove ""<div class ="error_message">" tag, it works perfectly. So the problem is that I am probably not using div tag correctly. Commented Jul 27, 2016 at 10:09
  • See my answer because of this, @user3369592 ;) Commented Jul 27, 2016 at 10:10

1 Answer 1

2

You code is fine in general, and working. The only thing went wrong, is the html escaping of the replace element:

// before
$('#replace').replaceWith("<div class ="error_message">"+ data.body +"</div>");

// after
$('#replace').replaceWith('<div class="error_message">' + data.body + '</div>');

And you should really update your jQuery version. 1.6 is a hundred years old! :)

Working example.

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

2 Comments

I agree with you..but our framework is still using 1.6...I can not update it by myself..
This is up to you. ;) But it even works with 1.6, I've tested it. @user3369592

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.