简体   繁体   中英

Using HTML5 Custom Data Attributes to pass data from <div to javascript

I want to pass data from a div id to a javascript highcharts function. I am following the advice kindly suggested by someone on here by looking at HTML5 Custom Data Attributes

However something must be wrong because when I try to use variables instead of numbers in the data series using val1, val2 etc nothing gets drawn. When I change it back to any number for test purposes it works. Can anyone explain why?

(I have left one as val1 here but the other 3 in the series are left as numbers)

THE JAVASCRIPT FUNCTION

<script>
$(function () {

    var piedata = document.getElementById('containerpie');
    var val1 = piedata.getAttribute('data-param1');
    var val2 = piedata.getAttribute('data-param2');
    var val3 = piedata.getAttribute('data-param3');
    var val4 = piedata.getAttribute('data-param4');

$('#containerpie').highcharts({



    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    },
    title:{
    text: 'Quality of teaching'
    },
    credits: {
        enabled: false
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                color: '#000000',
                connectorColor: '#000000',
                format: '<b>{point.name}</b>: {point.percentage:.1f} %'
            }
        }
    },
    series: [{
        type: 'pie',
        name: 'Judgement',
        data: [
            ['1',   val1],
            ['2',   24],
            {
                name: '3',
                y: 8,
                sliced: true,
                selected: true
            },
            ['4',    3]
        ]
    }]
});
});
</script>

THE DIV ID SECTION

<div id="containerpie" data-param1="10" data-param2="20" data-param3="30" data-param4="30" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

First of all, if you're using jQuery, why dont you use $("#containerpie") rather than getElementById? Also you can get the values with $("#containerpie").data("param1").

Are you sure var1, var2, etc. are being assigned? You could put an alert after the assignation to check. Also, try parsing the value to int (var val1 = piedata.getAttribute('data-param1');

Try this:

$(function () {

    var $piedata = $("#containerpie");
    var val1 = parseInt($piedata.data('param1'));
    var val2 = parseInt($piedata.data('param2'));
    var val3 = parseInt($piedata.data('param3'));
    var val4 = parseInt($piedata.data('param4'));

    alert("Val1: " + val1 + " Val2: " + val2 + " Val3: " + val3 + " Val4: " + val4 );

    $piedata.highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title:{
        text: 'Quality of teaching'
        },
        credits: {
            enabled: false
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Judgement',
            data: [
                ['1',   val1],
                ['2',   24],
                {
                    name: '3',
                    y: 8,
                    sliced: true,
                    selected: true
                },
                ['4',    3]
            ]
        }]
    });
});

Update:

I've run your code and it works fine, please see: http://jsfiddle.net/7ae7t/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM