-1

I am trying to pass the id which is

var aid = post.id;

to the message container and when the a onClick is being clicked, the page should alert the aid value. Other records are being displayed properly. However, the problem here is that it is saying aid is undefined. How do i fix this issue?

var message_container = '<div class=\"new-update clearfix\"><div class=\"update-done\"></br> </div> <a onClick="reject('+ aid + ');" id="aid" class=\"tip\" ><i style=\"font-size: 2em; float:right\"  class=\"icon-remove\"></i></a> </br></br> </div>';
        $.ajax({
        url : '/test/back-api/admin/announcements',
        method : 'GET',
        success : function(d) {
            if (d.result) {
                var posts = d.data;
                for (var i = 0; i < posts.length; i++) {
                    var post = posts[i];
                    var aid = post.id;
                    //message.find('#aid').append("href", post.id);
                    $("#announcement-container").prepend(message);
                }
            } else {
                $('#announcement-container').append("<div> Nothing to show. </div>");
            }
        }
});
var reject = function(aid){
     $.ajax({
     url: app.api + "admin/transaction/remove/"+aid,
     type: 'post',
     success: function(d){
       alert(aid);
     }
})
Filnor
  • 1,290
  • 2
  • 23
  • 28
John Wick
  • 19
  • 1
  • 1
  • 8
  • Define `aid` outside `ajax` call and assign value in success. It's all about `scope` of variable. – Fawkes Aug 16 '17 at 07:17

2 Answers2

1

If you use aid in an onxyx-attribute-style event handler, it has to be a global. That's one of many reasons not to use them.

Instead, hook up the event handler with modern handling, store the aid on the link as a data-* attribute, and retrieve it in reject:

// *** Removed `onClick` and `id="aid"` from this string
var message_container = '<div class=\"new-update clearfix\"><div class=\"update-done\"></br> </div> <a class=\"tip\" ><i style=\"font-size: 2em; float:right\"  class=\"icon-remove\">remove link</i></a> </br></br> </div>';
$.ajax({
    url : '/test/back-api/admin/announcements',
    method : 'GET',
    success : function(d) {
        if (d.result) {
            var posts = d.data;
            for (var i = 0; i < posts.length; i++) {
                var post = posts[i];
                var aid = post.id;
                //message.find('#aid').append("href", post.id);
                // *** Hook it up and prepend it
                $(message_container)               
                    .find("a.tip")
                        .attr("data-aid", aid)
                        .on("click", reject)
                        .end()
                    .prependTo("#announcement-container");
                // *** End of added bit
            }
        } else {
            $('#announcement-container').append("<div> Nothing to show. </div>");
        }
    }
});
function reject() {
    var aid = this.getAttribute("data-aid"); // *** Get the aid from the element
    alert("reject aid = " + aid);
}

Note that I removed the id="aid" from the string. You can't have the same ID on more than one element.

Live example:

// *** Removed `onClick` and `id="aid"` from this string
var message_container = '<div class=\"new-update clearfix\"><div class=\"update-done\"></br> </div> <a class=\"tip\" ><i style=\"font-size: 2em; float:right\"  class=\"icon-remove\">remove link</i></a> </br></br> </div>';
fakeAjax({
    url : '/test/back-api/admin/announcements',
    method : 'GET',
    success : function(d) {
        if (d.result) {
            var posts = d.data;
            for (var i = 0; i < posts.length; i++) {
                var post = posts[i];
                var aid = post.id;
                //message.find('#aid').append("href", post.id);
                // *** Hook it up and prepend it
                $(message_container)               
                    .find("a.tip")
                        .attr("data-aid", aid)
                        .on("click", reject)
                        .end()
                    .prependTo("#announcement-container");
                // *** End of added bit
            }
        } else {
            $('#announcement-container').append("<div> Nothing to show. </div>");
        }
    }
});
function reject() {
    var aid = this.getAttribute("data-aid"); // *** Get the aid from the element
    alert("reject aid = " + aid);
}
function fakeAjax(options) {
    setTimeout(function() {
        options.success({
            result: true,
            data: [
                {id: 1},
                {id: 2},
                {id: 3}
            ]
        });
    }, 300);
}
<div id="announcement-container"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you don't want to change reject, then just change the handler from:

.on("click", reject)

to

.on("click", reject.bind(null, aid))

and leave the rest the same.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I have tried this, it actually appends another 2 extra rows and didn't alert anything – John Wick Aug 16 '17 at 07:39
  • @JohnWick: I had planned to just leave it, since the solution you marked accepted does work even if it's quite poor. But I felt like I'd done a bad job, so I finished doing it. See above, including live example. – T.J. Crowder Aug 16 '17 at 08:11
0
var message_container = $('<div class=\"new-update clearfix\"><div class=\"update-done\"></br> </div> <a onClick="reject('+ aid + ');" id="aid" class=\"tip\" ><i style=\"font-size: 2em; float:right\"  class=\"icon-remove\"></i></a> </br></br> </div>');
$.ajax({
    url : '/test/back-api/admin/announcements',
    method : 'GET',
    success : function(d) {
        if (d.result) {
            var posts = d.data;
            for (var i = 0; i < posts.length; i++) {
                var post = posts[i];
                $(message_container).find('#aid').attr("onClick", "reject('"+ post.id + "')");
                $("#announcement-container").prepend($(message_container).clone(true));
            }
        } else {
            $('#announcement-container').append("<div> Nothing to show. </div>");
        }
    }
});
var reject = function(aid){
     $.ajax({
         url: app.api + "admin/transaction/remove/"+aid,
         type: 'post',
         success: function(d){
           alert(aid);
         }
    });
}

Please see the updated answer.

gjijo
  • 1,206
  • 12
  • 15