dtgee
dtgee

Reputation: 1272

Is it possible to set a HTML id as a json object and pass it to another function?

I pretty much just want to pass a json object over to another function when it happens (for example a click event gets called).

$(document).on('click', '.some_link', function() {
var something = "something";

    $.get('/someDirectory/someScript.php', {
        'some_var': something
    }}.done(function(data) {
        var json = jQuery.parseJSON(data);
        $('#json-item-container').val(json);
    });
});

$('.hidden-input').click(function() {
    var json = $('#json-item-container').val();
    //Do something with json
});

Upvotes: 1

Views: 162

Answers (2)

user1403947
user1403947

Reputation:

Store the json data in the global scope:

var json; // json var defined in global scope

$(document).on('click', '.some_link', function() {
var something = "something";

    $.get('/someDirectory/someScript.php', {
        'some_var': something
    }}.done(function(data) {
        json = data;
    });
});

$('.hidden-input').click(function() {
    console.log('Ohai! Im the json object: ' + JSON.stringify(json));
});

Upvotes: 1

bdukes
bdukes

Reputation: 156075

Use jQuery's data method, instead of val, to store arbitrary data associated with an element.

Upvotes: 2

Related Questions