1

I have a function that will be triggered on click (using jQuery).

I don't understand why the following doesn't work and shows undefined.

var testFunc = function(event) {
    var data     = event.data;
    var category = data.category;
    var action   = data.action;
    var label    = data.label;

    console.debug(data);
    // prints:
    // {'category': 'CCC', 'action': 'AAA', 'label': 'LLL'}

    console.debug(data.category);
    // prints: undefined

    console.debug(category);
    // prints: undefined

    console.debug(data[category]);
    // prints: undefined
};
$(document).ready(function() {
    $('.test').on('click', function() {
        var jsonData;
        try {
            jsonData = JSON.parse($(this).data('test'));
        }
        catch (e) {
            // I guess here is the problem:
            jsonData = $(this).data('test');
            // ..but why JSON.parse doesn't work
        }

        try {
            testFunc({ data: jsonData });
        } catch (e) {
        }
    });
});

HTML:

<a href="about:blank" target="_blank" class="test"
   data-test="{'category': 'CCC'}">click event triggered here</a>

How to access event.data.category ?

FIDDLE: http://jsfiddle.net/ytqp45wj/

3
  • Can you repro in a fiddle? Commented Jun 1, 2015 at 14:35
  • 1
    what is the typeof data? Commented Jun 1, 2015 at 14:36
  • Is data a plain object, or some structure that implements toString differently ? Commented Jun 1, 2015 at 14:36

4 Answers 4

2

event.data is a string in your fiddle.

That's because your JSON in the data-test field is not valid. So you pass into the exception handler when parsing it, and you use the String itself.

(Seems like you can not use single quotes in JSON, according to http://jsonlint.com/)

As pointed elsewhere, you can use double quotes inside your data attribute to fix things.

Note that the data[category] would not work anyway, so you still get undefined for that.

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

1 Comment

Tricky ! Solved it by changing the quotes like data-test='{"json":"val"}'
2

Your JSON data isn't formatted correctly. You need to use double quotes for the key values.

<a href="about:blank" target="_blank" class="test" data-test='{"category": "CCC", "action": "AAA", "label": "LLL"}'>click event triggered here</a>

Comments

0
'use strict';

var event = {data:{
     category: 'CCC', 
     action:'AAA', 
     label: 'LLL'
}};


var testFunc = function(event) {
    var data = event['data'];
    var category = event['data']['category'];


    console.debug(data);
    console.debug(category);

    //get keys for every data entry
    Object.keys(data).forEach(function(key){
        console.log(key, data[key]);
    });
};

testFunc(event);

JSFiddle

You need to have a valid JSON object so you need to access to it using JSON notation.

1 Comment

I made an ammed to my post.
0

To access nested object properties use [] notation, for example:

event['data']['category']

1 Comment

There is no reason why you should have to use bracket notation instead of dot notation unless there are spaces in the var name

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.