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.
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"}" .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.