Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i implement search like ctrl+f with jquery

Hi i am trying to search on page table tbody, the search is should be like ctrl + F search this my html and php code:

Please refer the image and link linked below for the sources.

    <table>
    <thead>
    <tr><td><input type="text" placeholder="Search Student id="searchStudent"> </td>
    <td></td><td></td><td></td><td></td><td></td></tr>
    </thead>

    <tbody>
    <tr>
    <td>
     <span><img src="<?php echo $gdScoreData['profile_pic']; ?>"/></span>
        <p><?php echo $gdScoreData['student_fname'];?> <?php echo $gdScoreData['student_lname'];?></p>
<p><b>Hallticket</b> : S<?php echo $gdScoreData['student_pid']; ?>J<?php echo $jobID; ?></p>
         </td>
    <td></td><td></td><td></td><td></td><td></td></tr>

1st tr will close here php code is the above one in html it looks like shown in image:

<tr><td><span><img src="profile_pic/first_1479536519.jpg" width="50" /></span><p>Robert Smith</p><p><b>Hallticket</b> : S27J2</p></td></tr>
</tbody>
</table>

This my javascript code to search like ctrl + F

/* Search Student like ctrl+f start */
$("#searchStudent").on("keyup", function() {
    var value = $(this).val();
    console.log(value);
    $("table tbody tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $row.find("td:first").text();

            if (id.indexOf(value) !== 0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});
/* Search Student like ctrl+f end*/

enter image description here

Here is the source from where i have tried: Source JS

like image 922
Mr world wide Avatar asked Feb 05 '23 05:02

Mr world wide


1 Answers

i have modified your js fiddle code now it working jsfiddle.net/rFGWZ/3406/

       $("#search").on("keyup", function() {
        var value = $(this).val();
       if(value!='') {
           $("table tbody tr").hide();
       }else{
           $("table tbody tr").show();
       }
         $('table tbody tr td:contains("'+value+'")').parent('tr').show(); 
       });
like image 87
Zeeshan Mahboob Avatar answered Feb 07 '23 18:02

Zeeshan Mahboob