views:

25

answers:

3

I am trying to pass a variabel from a .click event to a colorbox plugin like so:

$('.w_price_assess p.price_report > a').live('click', function() {

    var $reportRef = $(this).attr('href');

    var $reportID = $reportRef.substr($reportRef.lastIndexOf('/') + 1);

    return false;

});

I need $reportID to be visible to an AJAX call to form part of a URL.

Can this be done? My knowledge of doing this is limited obviously :(

+2  A: 

How about declaring the variable outside of click event and giving it public visibility:

var $reportID = null;

$('.w_price_assess p.price_report > a').live('click', function() {

    var $reportRef = $(this).attr('href');

    $reportID = $reportRef.substr($reportRef.lastIndexOf('/') + 1);

    return false;

});

Now you can use $reportID elsewhere in your script too.

Sarfraz
Can this be used to pass to the colorbox AJAX call as above?
RyanP13
@RyanP13: Yes, make sure that you declare that variable before anything else preferably after `<script>` starting tag.
Sarfraz
A: 

$.data() to store data globally.

http://api.jquery.com/data/

$.data(document.body, "myData", f.substr($reportRef.lastIndexOf('/') + 1)); 
jAndy
A: 

I am trying to pass $reportID to a colorbox plugin like so:

// Colorbox dialog window for price report
// Once dialog loaded append navigation header
$('.w_price_assess p.price_report > a').colorbox({
    title: 'Price report',
    transition: 'elastic',
    innerWidth: '800px',
    innerHeight: '650px',
    opacity: '0.5',
    onComplete: function() {
        // Call the dialog header and append
        $.ajax({
            type: 'GET',
            url: DashboardApplicationRoot + 'PriceReport/' + $reportID + '/header',
            dataType: 'html',
            cache: false,
            success: function(data) {
                $('#cboxTitle').append(data);
                // re-initialise form styling
                //$('form.jqtransform').jqTransform();                    
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }

        });

        // re-initialise scroll bars
        $('#cboxLoadedContent').jScrollPane();

        $('#cboxLoadedContent table').removeAttr('width');

        $('.Section1').wrap('<div class="print_me" />');

        $('.Section1').css({
            'width': '95%',
            'margin': 'auto'
        });

        $('.Section1 table').css({
            'width': '100%',
            'margin': 'auto'
        });

        $('#cboxLoadedContent > div').addClass('dialog_loaded');

    },

    onClosed: function() {

        // fixes scroll bar duplication bug
        $('#cboxContent .jScrollPaneContainer').remove();

    }

});
RyanP13