views:

88

answers:

2

i want to save javascript object in a place, so if selected row is getting trigger, i can use that row object again by different methods.

maybe saving it in input hidden field can work? but not sure.. how would you do it?

im trying to do following, but that dont work, obviously my code is wrong, but i want to show you, so you can maybe tell proper way to do it.

<script>
    jQuery(function($) {

        var video = {title: 'this is title', time: '2:32:20'}
        $('.arr').val(video);

        $('.show').click(function() {
            console.log($('.arr').val());
        });
    });
</script>
<input type="hidden" name="arr" class="arr" value="" />
<input type="button" class="show" value="Show" />
+4  A: 

JQuery has support for element data (see http://docs.jquery.com/Core/data). This allows you to set data in a "known" spot, like the document itself to be retrieved later.

$(document).data('foo','my data');

which can be retrieved as:

$(document).data('foo') // 'my data'

You are not limited to string values, objects can also be stored.

Hope this helps.

cjstehno
+2  A: 

If I understand right, you can save these objects you got from the "json server" using the jquery .data() method. If the object you want to save is in a variable named myobject and you want to save it with a DOM element with id "someid"

$('#someid').data('mydata', myobject);

saves the data. To retrieve:

var thedata = $('#someid').data('mydata');
fsb
i voted you and him, but you both are right.. sence he answered first, so ill go with him. but thank you :)
Basit
It's kind of a bummer there is no way to merge answers that are the same and share points or something... might be a good new feature.
cjstehno