CostCare CostCare - 7 months ago 18
Javascript Question

Change function to use as onchange event

I have the following

javascript
function for listbox organization:

$("#organization").change(function(e) {
//function updateOrganization() {
organization = $("#organization").val();
indicator = $("#indicators").val();
funcid = "fill_chart_average_score";

//console.log('changed');
$.getJSON('functions/getfunctions.php', {
"organization":organization,
"indicator":indicator,
"funcid":funcid},

function(dataChart) {

var chart = AmCharts.makeChart("chartaveragescore", {
"theme": "light",
"type": "serial",
"startDuration": 1,
"dataProvider": dataChart,
"rotate": false,
"categoryField": "organisatie",
"valueAxes": [ {
"gridColor": "#FFFFF",
"gridAlpha": 0.2,
"dashLength": 0
} ],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [ {
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillColorsField": "fillcolor", //Dit veld heb ik meegegeven vanuit SQL functie en bevat de HEX kleurcodes BD
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "score"
} ],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
},
"export": {
"enabled": true
}

},0);

$('.chart-input').off().on('input change',function() {
var property = jQuery(this).data('property');
var target = chart;
chart.startDuration = 0;

});
});
});


This works great, but what i want now is to make a function not based on
$("#organization").change(function(e)
but just make a function like
function updateOrganization()
and call this function in an
onchange
within the
html
element. When i change the
$("#organization").change(function(e)
with
function updateOrganization()
the function doesn't work anymore.

I'm a starting
javascript
learner :).

Answer

You should declare your function outside the block. Something like:

function updateOrganization() {
    organization = $("#organization").val();
    indicator = $("#indicators").val();
    funcid = "fill_chart_average_score";

    // ...
}

and then use it as:

$("#organization").change(updateOrganization);