How to pass parameters to callback functions in javascript HTML events

Asked

Viewed 18 times

1

Hello, good evening, I’m having trouble with callback functions in javascript and need help.

The following lines below deal with rows in an HTML table, if the element present in cell2 is a composite element, I add a + button so that it can be expanded by the user and use the callback click function.

This code is inside a loop, which creates the lines and stores in firstrow.

What I want is for the firstrow die within the callback to represent the moment it was created.

But that’s not what’s happening, regardless of which icon I click the firstrow value always contains the last iteration element.

How do I pass some parameter to callback function?

firstRow.setAttribute("class", "clickable");
    firstRow.setAttribute("data-toggle", "collapse");
    firstRow.setAttribute("data-target", ".row"+objeto.idtab);
    firstRow.setAttribute("id", "linha_"+objeto.idtab);
    cell2.innerHTML = "<i class='glyphicon glyphicon-plus'></i><div id='variableToggle_"+objeto.idtab+"'>";
    cell1.innerHTML = objeto.Nome;
    cell2.addEventListener("click", function(offset, firstRow){
        if(show){
            show = false;
            cell2.innerHTML = "<i class='glyphicon glyphicon-plus'></i><div id='variableToggle_"+objeto.idtab+"'>"
            var i = finalRow.rowIndex;
            while(i > firstRow.rowIndex){
                table.deleteRow(i);
                i--;
            }
        }
        else{
            finalRow = insertVariableFields(objeto.idtab, ref, firstRow);
            show = true;
            cell2.innerHTML = "<i class='glyphicon glyphicon-minus'></i><div id='variableToggle_"+objeto.idtab+"'>"

        }
        console.log('abriu');
    });
  • Hello Jacons! Take a look at the other question that Linkei. If you can’t implement it says that I give more help here in the comments.

  • Hello, it was really helpful the link, I thought it was some problem with the closure but was not finding an alternative, the example 2 of the answer was useful for me, thanks.

  • Great! See you soon!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.