0

I can't fingure out why this code doesn't work:

$(document).ready(function() { 

    $(".alertDeleteButton").click(function(e) {

        var str = $(this).attr("href");
        alert(str);

        var obj = str.split("&").reduce(function(prev, curr, i, arr) {
            var p = curr.split("=");
            prev[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
            return prev;
        }, {});

        alert(obj);


        e.preventDefault(); 
    });

}); 

jsFiddle

All I want to do, is use the string in in a link when pressed by a user and create a json object.

{"userdeleteid": "55", "dayid": "55"}. etc...

I've searched and found about 3 different ways to do it, and they ALL give me the [object Object] result.

I wanted to implement this, but it seems to only look at the URL in the browser, I want it to look at the value of the link via (this).attr("href");

1
  • alert() prints strings so it needs to convert the object to string. You should debug with Firebug's console or your browser's equivalent tool. Commented Feb 28, 2013 at 16:38

2 Answers 2

2

You should use JSON.stringify(), like this:

alert(JSON.stringify(obj));
Sign up to request clarification or add additional context in comments.

1 Comment

God damn it, I've literary spent the last 3 hours on this, refusing the ask a question on here because I was sure the answer was right in front of me... All 3 versions that I got to work and only got [object Object] WORK, I just needed your line. Thank you for saving my sanity!
1

You can use JSON.stringify available as part of the modern browser API If you are dealing with old browsers please refer JSON 2 (Third party support library).

enter image description here

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.