0

I have a JQuery that records all the immediate parent lists when a child list is clicked.

Example -

- Parent1  
  - Child1
     - GrandChild1
     - Grandchild2
     - Grandchild3
  - Child2
     - Grandchild4
     - Grandchild5

Clicking on Grandchild2 will record Parent1, Child1 and Grandchild2.

I want to use JQuery, JS and Cookies to print a value in another page when something is clicked in this list page. However the following codes doesn't work. Kindly help.

JQUERY and JS Code - (for the list page)

function objToString (obj) {
            var str = '';
            for (var p in obj) {
                if (obj.hasOwnProperty(p)) {
                    str += p + '::' + obj[p] + '\n';
                }
            }
            return str;
        }

        $(document).ready(function() {
            $('li').click(function() {
                var obj = $(this).parents('li').add(this);
                obj.css('color', 'red');
                var data= "data=";
                document.cookie = data+objToString(obj);
            });
        });

PHP Code - (for the page where the list data is to be printed)

echo $_COOKIE['data'];

The PHP Code should print Parent1 Child1 Grandchild2 for the above example. Also all functionalities should be IE 7 compatible. Only problem I face is that objToString doesn't work properly here.

12
  • You're defining $obj but sending obj to the objToString method. Commented Dec 28, 2013 at 4:26
  • 1
    why aren't you using JSON.stringify? Commented Dec 28, 2013 at 4:28
  • @Jason P >> Following your advice it prints now 0::[object HTMLLIElement]. Still not printing the required result. Commented Dec 28, 2013 at 4:30
  • @Markasoftware >> Thanks for your reply. I never heard of JSON.stringify. Can you please elaborate this example with it? Commented Dec 28, 2013 at 4:31
  • JSON is an object stringification system used to turn objects and other variables into strings Commented Dec 28, 2013 at 4:34

1 Answer 1

1

Use JSON.stringify instead. This function converts any Object to String, concatinating the keys and values.

Example:-

var obj = {a:"b", f:{c:"d"},e:"e"};
JSON.stringify(obj); 

Output:-

"{"a":"b","f":{"c":"d"},"e":"e"}" .

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

16 Comments

I used document.cookie = data+JSON.stringify(obj); but it says Uncaught TypeError: Converting circular structure to JSON directory.php:76 (anonymous function) directory.php:76 jQuery.event.dispatch jquery-1.9.1.js:3074 elemData.handle at the Java console. I included the script as <script src="json2.js"></script>
This might get some issue with HTML Nodes, like if you are doing some thing like. JSON.stringify($('div')); This will get Circular Referance error.
I have edited the code a bit. I have defined the object as obj now. document.cookie = data+JSON.stringify(obj); still shows no luck. Any guess what to do now?
The Circular structure error.. comes.. due to the following reason.. as HTML Nodes have circular references. Since every node has a property 'ownerDocument', which means every node is referring to the document and document node refers to this node via child relation ship. So it islike node->document-> node. This is a very basic case, in case event listeners there can be multiple loops.
Simple example of Circular Reference: var a = {}; a.b = a; JSON.stringify(a); This will give the same error as you are getting inyour case.
|

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.