Select GridView line with checkbox

6

    function Selecionar(elemento,cordefundo)
        {
            var Inputs = elemento.getElementsByTagName("input");
            var cor = elemento.style.backgroundColor; //manter a cor default do elemento
            for(var i = 0; i < Inputs.length; ++i)
            {
                if(Inputs[i].type == 'checkbox')
                {
                    Inputs[i].checked = !Inputs[i].checked;
                    elemento.style.backgroundColor = cordefundo;
                    elemento.onclick = function()
                    {
                        Selecionar(this,cor);
                    };         
                }         
            }       
        }
protected void gvSelecao_PreRender(object sender, EventArgs e){
        GridView gv = (GridView)sender;
        foreach (GridViewRow row in gv.Rows)
            row.Attributes.Add("onclick", "Selecionar(this,'#FFFF00');");
        }

I took these two codes from a website to make you select gridview rows by clicking on them.

This part I did not understand

 elemento.onclick = function()
                {
                    Selecionar(this,cor);
                };         

Why does he call the function again? Actually I understand, call the function again because it is part of the logic: it returns the default color of the line and unchecks the ckbox.

But I do not understand, because for me when the function was called it was closed, but when debugging I saw that when I click again on the line it falls directly into that part of the function:

elemento.onclick = function () {
                        Selecionar(this, cor);
                    };

What I found a mystery, because as I said, for me the function was called and closed. I took a print of what appears when I click again on the line:

Does anyone explain to me how this works? Why does not the function terminate? If they were several functions, would they remain open?

    
asked by anonymous 11.10.2015 / 21:30

1 answer

1

Willian,

it is not calling the function again. The snippet that you are in doubt means:

If there is the click event on the element, then call the Select method (this, color) again. The this parameter means the element that has identified the click event on the screen. The name of this is Callback and is very used in Javascript.

This explains why when you click on the element again the first execution is the part of the part of the function;

I hope I have helped.

    
11.02.2016 / 15:09