1

I've been stuck on this for the last day and a half. I am trying get a real time filter added for my html table (which is generated off a PHP script to import a .csv file to html standard markup). I based my code off the following demo http://jsfiddle.net/7BUmG/2/ that I found from How to perform a real time search and filter on a HTML table .

Now I think the issue is that the elements are not defined at time of DOM. I've tried adding the following to accommodate for this but with no luck.

<script>
$(document).ready(function() {   
// My javascript  
});
</script>

and

<script>
$(document).on('pageinit')  {  
// My javascript  
});
</script>

and also just a plain function tag

<script>
$(function(){  
// My javascript  
});
</script>

Does anyone know how to get this js code to perform upon keystroke in my html page? (it works in the link provided above but not outside jsfiddle)

$('#search').keyup(function() {
    var $rows = $('#table1 tr');
    var val = '^(?=.*\\b' + $.trim($('#search').val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
        reg = RegExp(val, 'i'),
        text;
    $rows.show().filter(function() {
        text = $(this).text().replace(/\s+/g, ' ');
        return !reg.test(text);
    }).hide();
});

Ps. This is my first post so let me know if I did something wrong or need more explanation given. Thanks!

Community
  • 1
  • 1

1 Answers1

0

Are you sure you're binding JS events after your DOM tree is built?

E.g. wrap your code as ready event listener of the document

$(document).ready(function(){

    $('#search').keyup(function() {
        //...
    }

})
Te Ko
  • 768
  • 3
  • 13
  • Yes this is what I currently have. $(document).ready(function() { //$(function(){ $('#search').keyup(function() { var $rows = $('#table1 tr'); var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$', reg = RegExp(val, 'i'), text; $rows.show().filter(function() { text = $(this).text().replace(/\s+/g, ' '); return !reg.test(text); }).hide(); }); }); – LearnerMike Dec 13 '13 at 18:33