views:

29

answers:

2
    $(document).ready(function(){
        $('#cumulative-returns').graph({
            width: 400,
            height: 180,
            graphtype: 'bar'
        });
    });

I have a binded a function on click to #cumulative-returns and I want to be able to get the graphtype value like..

$('#cumulative-returns').click(function() {
  alert(this.graphtype)
});

Is this possible or how else would you go about it? Maybe have some code in the graph function that stores the parameters in some global array (messy but feasable)?

*edit: here is the graph function:

(function($) {
    $.fn.graph = function(options) {
        return this.each(function() {
            var defaults = {
                name: $(this).attr('id')
            };
            var opts = $.extend(defaults, options);
            var img = this;
            $.post('../generate_graph.php', opts);
        });
    };
})(jQuery);
A: 

You do get the dom element that has been clicked stored in this. Then I don't know if it has the graphtype method linked to it, it all depends on how .graph() behaves.

marcgg
A: 
(function($) {
    $.fn.graph = function(options) {
        return this.each(function() {
            var defaults = {
                name: $(this).attr('id')
            };
            var opts = $.extend(defaults, options);
            var img = this;

            $(this).data('graphtype', opts.graphtype); // Addition!        

            $.post('../generate_graph.php', opts);
        });
    };
})(jQuery);

Then just do:

$('#cumulative-returns').click(function() {
  alert($(this).data('graphtype'));
});
Matt
while you're at it, add a fallback to the extend. `var opts = $.extend(defaults, options || {});`
jAndy
@jAndy: jQuery does this itself.
Matt