简体   繁体   中英

Change the code of a javascript function

Actually my idea is the following: i have a div tag that displays the result of a function called "afficher_general()" inside a script tag, and a button that calls a first function called "Ajouter_general()" that's supposed to change a string inside the function "afficher_general()"

var general = document.getElementById("general");
general.innerHTML = general.innerHTML.replace('//WRITE', ch);

the button is as follows:

<input type="button" value="ajouter au graphe general" id="id1" onClick="Ajouter_general();afficher_general();" />

But this doesn't seem to work because i've been told that a script is loaded only once and it cannot be changed. So is there any solution ? Thanks in advance.

So i'll add what contain the functions. Actually i'm working with Amcharts . the function afficher_generalis as follows:

<script name="general" id="general" type="text/javascript">
    function afficher_general()
    {

                    chart = new AmCharts.AmSerialChart();
                    chart.dataProvider = chartData;
                    chart.categoryField = "country";
                    chart.startDuration = 1;                
                    // WRITE
                    chart.write("chartContainer");


    }
</script>

instead of the comment //WRITE i wish to add the a code everytime the button is pressed. the function is as follows:

function Ajouter_general(ind){
var ch="";
var couleur = getSelectValueId("background-color"+ind);
var transparence = getSelectValueId("transparence"+ind);
var type = get_radio_value_type(ind);
var balloon = getSelectValueId("balloon-color"+ind);
if(type == "column")
                {
                ch=ch+"var graph"+ind+" = new AmCharts.AmGraph();";
                                ch=ch+"graph"+ind+".valueField = "+"visits"+";"
                ch=ch+"graph"+ind+".balloonColor = "+balloon+";";
                                ch=ch+"graph"+ind+".type = "+type+";";
                                ch=ch+"graph"+ind+".fillAlphas = "+transparence+";";
                ch=ch+"graph"+ind+".fillColors = ["+couleur+"];";
                ch=ch+"graph"+ind+".lineColor = ["+couleur+"];";
                ch=ch+"chart.addGraph(graph"+ind+");";
                ch=ch+"// WRITE";
                }
var general = document.getElementById("general");
general.innerHTML = general.innerHTML.replace('//WRITE', ch);
}

since Amcharts uses the script to draw graphs inside a chart, i would like to be able to dynamically add those graphs everythime the button is pressed.

If you really need to change the definition of a function, you could always just redefine it (functions are just objects assigned to variables):

var Ajouter_general = function () {
    // redefine the afficher_general function
    afficher_general = function () {
        // change it to whatever you want
    };
};

That being said, this is generally a bad practice and the same functionality can be better accomplished by using function parameters.

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