Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
Using javascript like a function with button

I want to use this code as a function () to be run only when I click a button and not automatically when dropdown is changed. When click on <button onclick="CalendarFilter">Cercar</button> it runs the function. I tried putting the code into this but not working.

function myFunction()
{
CODE HERE
} 





$(document).ready(function () {

    $('.filter').change(function () {
        var values = [];

         $('.filter').each(function () {
            var colIdx = $(this).data('col');

             $(this).find('option:selected').each(function () {
                 if ($(this).val() != "") values.push( {
                    text: $(this).text(),
                    colId : colIdx
                 });
            });
        });
        filter('table > tbody > tr', values);
    });

    function filter(selector, values) {console.log(values);
        $(selector).each(function () {
            var sel = $(this);
            var tokens = sel.text().trim().split('\n');
            var toknesObj = [], i;
            for(i=0;i<tokens.length;i++){
                toknesObj[i] = {
                   text:tokens[i].trim(), 
                   found:false
                };
            }

            var show = false;
            //console.log(toknesObj);
            $.each(values, function (i, val) {                

           if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {
               toknesObj[val.colId].found = true;
              }

            });          
            console.log(toknesObj);
            var count = 0;
             $.each(toknesObj, function (i, val) {
                 if (val.found){
                     count+=1;
                 }
             });
            show = (count === values.length);        
            show ? sel.show() : sel.hide();
        });
    }
    });

When click on Cercar it runs the CalendarFilter

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I need a way to run this like this:

function myFunction()
{
        $(document).ready(function () {
        
            $('.filter').change(function () {
                var values = [];
        
                 $('.filter').each(function () {
                    var colIdx = $(this).data('col');
        
                     $(this).find('option:selected').each(function () {
                         if ($(this).val() != "") values.push( {
                            text: $(this).text(),
                            colId : colIdx
                         });
                    });
                });
                filter('table > tbody > tr', values);
            });
        
            function filter(selector, values) {console.log(values);
                $(selector).each(function () {
                    var sel = $(this);
                    var tokens = sel.text().trim().split('\n');
                    var toknesObj = [], i;
                    for(i=0;i<tokens.length;i++){
                        toknesObj[i] = {
                           text:tokens[i].trim(), 
                           found:false
                        };
                    }
        
                    var show = false;
                    //console.log(toknesObj);
                    $.each(values, function (i, val) {                
        
                   if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) {
                       toknesObj[val.colId].found = true;
                      }
        
                    });          
                    console.log(toknesObj);
                    var count = 0;
                     $.each(toknesObj, function (i, val) {
                         if (val.found){
                             count+=1;
                         }
                     });
                    show = (count === values.length);        
                    show ? sel.show() : sel.hide();
                });
            }
            });
    
}

To call that function using a button.

<button onclick="myFunction()">Cercar</button>

The problem is that this way is not working.

Thanks.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda