2

I am having some problems while creating a dynamic webpage in javascript.

My idea is to read a list of events and people signed up on them. I create a page with all events (each event is a button) and clicking on one of them, see the list of users.

This works fine. But now, I am adding a button to export some of these users to an excel file. And I want to add a button with an onClick function like this:

...onclick=functionÇ(id_Event, numberOfUsers, listOfUsers)...

Inside of the html code generated by javascript. I found some problems also doing like this so I changed so:

var td = document.createElement("td");
var input = document.createElement("input");
input.setAttribute("type","button");
input.setAttribute("value","Exportar a Excel CSV");
input.onclick = function() {
    saveExcelFunctionControl(arrayNumberUsersInEvents[i], response);
};

td.appendChild(input);
document.getElementById("added"+element[i].id_Event).appendChild(td);

I created a global array called arrayNumberUSersInEvents in which I am adding in each possition, people subscribed. i, is the id counter for each position.

But even this, I am getting an undefined while reading the value of the firsdt parameter. I think it is a problem of dynamic data, I am not executing the function I want to each time I click the button. Do you know how to do something like this?

To sum up: My problem is that I want to pass some arguments to a function in a dynamic created page. I don't know how to pass the data and read the correct parameters inside.

I added my code because one user asked for it:

for(i = 0; i < element.length; i++){
                                             $(".eventsControl").append(
                                        '<li     id="listControl'+ element[i].id_Event +'">'+
                                    '<a href="#EventControl' + element[i].id_Event + '"' + 'data-transition="slidedown">'+
                                    '<img class="crop" src= "' + element[i].image + '"  />'+
                                    '<h2>' + element[i].name + '</h2>'+
                                    '<p>' + "Desc:  " + element[i].description +'</p>'+
                                    '</a>'+
                                    '</li>'
                                ).listview('refresh');
                         //console.log(response);

                            //BUCLE for setting all users in each event. Better use some string and after, join all of them
                                header = ' <div  width="100%" data-theme = "e" data-role="page" id='+ element[i].id_Event +
                                 ' data-url="EventControl' + element[i].id_Event + '"> ' +
                                 ' <div data-theme = "a" data-role="header"><h1>Lista de Asistencia</h1> ' + 
                                 ' <a href="#controlList" data-icon="back" data-iconpos="notext"></a></div>'+
                                 ' <div data-role="content"> ' + 
                                 ' <fieldset data-role="controlgroup" data-type="horizontal" style="text-align: center">' +
                                 ' <div style="width: 500px; margin: 0 auto;">';

                                 //header = header + '<input data-theme = "c" onclick="saveExcelFunctionControl(this)" id="saveExcelControl' + element[i].id_Event + '" type="button" value = "Guardar a excel"></br>';

                                eval('var numberUsers' +element[i].id_Event + "=1");
                                arrayNumberUsersInEvents[i] = 0; 
                                if(response.length>0){
                                    bucle = ' <table width="100%" border="1" align="left"><tr>'+

                                            ' <th>Nombre</th>'+
                                            ' <th>Primer apellido</th>'+
                                            ' <th>Segundo apellido</th>'+
                                            ' <th>NIF</th>'+
                                            ' <th>Asistencia</th>'+
                                            ' </tr>';

                                    for(iData = 0; iData < response.length; iData++){
                                        if(element[i].id_Event == response[iData].id_Event){
                                            //console.log(response[iData].name);
                                                bucle = bucle + '<tr><td>'+ eval('numberUsers' +element[i].id_Event) +'</td><td>'+ response[iData].name +'</td><td>'+ 
                                                response[iData].surname1 +'</td><td>'+ 
                                                response[iData].surname2 +'</td><td>'+ 
                                                response[iData].NIF + '</td>'+
                                                '<td> '+
                                                    '<input type="checkbox" id="checkBox'+element[i].id_Event+'_'+iData+'" name="option'+iData+'" value="'+iData+'">  '+
                                                '</td>'+


                                                '</tr>';    

                                            eval('numberUsers' +element[i].id_Event + "++");
                                            arrayNumberUsersInEvents[i] = arrayNumberUsersInEvents[i]+1;             
                                        }
                                    }
                                    //header = header + '<input data-theme = "a" onclick="saveExcelFunctionControl(\""element[i].id_Event "\","" + numberUsers + "\",\"" + response+ "\"")" id="saveExcelControl' + element[i].id_Event + '" type="button" value = "Guardar a excel"></br>';
                                    //header = header + '<input data-theme = "a" onclick="saveExcelFunctionControl(""+numberUsers+"")" id="saveExcelControl' + element[i].id_Event + '" type="button" value = "Guardar a excel"></br>';
                                    bucle = bucle + '</table>';
                                    $("#controlList").after(header + bucle + '<div id=added'+element[i].id_Event+'></div>');


var td = document.createElement("td");
var input = document.createElement("input");
input.setAttribute("type","button");
input.setAttribute("value","Exportar a Excel CSV");
input.onclick = function() {
    saveExcelFunctionControl(arrayNumberUsersInEvents[i], response);
};

td.appendChild(input);
document.getElementById("added"+element[i].id_Event).appendChild(td);


                                }


                }

                },
       error: function(xhr, status, message) { alert("Status: " + status + "\nControlGetEventsRegister: " + message); }
    });
4
  • And where is i coming from ? Commented Dec 9, 2013 at 10:14
  • i is a counter for reading all events. It plus 1 each row. So the first event in list will be 0, the second 1... Commented Dec 9, 2013 at 10:16
  • Then show us how you do that ? Commented Dec 9, 2013 at 10:16
  • I already added the code Commented Dec 9, 2013 at 10:19

2 Answers 2

2

You can use closure to pass parameters to dynamically created onclick handler:

input.onclick = (function() {
    var arr = arrayNumberUsersInEvents[i];
    var resp = response;
    return function() {
        saveExcelFunctionControl(arr, resp);
    }
})();

How do JavaScript closures work?

Sign up to request clarification or add additional context in comments.

3 Comments

Adding the last () makes the button doesn't appear and also the function is launch when the page loads
@Biribu last () just calls anonymous function that return onclick handler function.. Maybe error somewhere else
I couldn't got it working. I solved in other way. Anyway, I made this works in a simple example so I guess it works and I have other error I haven't seen yet. Solved tag goes for you. Thanks for your time
0
var td = document.createElement("td");
var input = "<input type='button' value='Exportar a Excel CSV'";
input+= "onclick='saveExcelFunctionControl(""" +arrayNumberUsersInEvents[i]+""","""+ response+""");' />";

};

td.textContent=input;
document.getElementById("added"+element[i].id_Event).appendChild(td);

try this way

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.