Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to make table appear

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to make table appear

    Hi all,

    Apologies I'm a completely beginner in JS. I have the following script that filters a table based on the the text as I type. However, my list will be quite long before anything is typed in the box. Therefore, how can i adjust the code so that the table only starts to appear once something is entered into the text box (without using a separate search button).

    -<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    * {
    box-sizing: border-box;
    }

    #myInput {
    background-image: url('/css/searchicon.png');
    background-position: 10px 10px;
    background-repeat: no-repeat;
    width: 100%;
    font-size: 12px;
    padding: 12px 20px 12px 40px;
    border: 1px solid #ddd;
    margin-bottom: 12px;
    }

    #myTable {
    border-collapse: collapse;
    width: 100%;
    border: 1px solid #ddd;
    font-size: 10px;
    }

    #myTable th, #myTable td {
    text-align: left;
    padding: 26
    px;
    }

    #myTable tr {
    border-bottom: 1px solid #ddd;
    }

    #myTable tr.header, #myTable tr:hover {
    background-color: #D9B300;
    }
    </style>
    </head>
    <body>

    <h2>Search by Building Number</h2>

    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Enter any part of your building number..." title="Type in an address">

    <table id="myTable">
    <tr class="header">
    <th style="width:20%;">House Number</th>
    <th style="width:20%;">Building Name</th>
    <th style="width:60%;">BuildingID</th>
    </tr>
    <tr><td>G-1000</td><td></td><td>8259-23955-4071</td>
    <tr><td>G-1001</td><td></td><td>8280-23955-4041</td>
    <tr><td>G-1002</td><td></td><td>8279-23955-4076</td>
    <tr><td>G-1003</td><td></td><td>8304-23955-4046</td>
    <tr><td>G-1004</td><td></td><td>8291-23955-4079</td>
    <tr><td>G-1005</td><td></td><td>8322-23955-4049</td>
    <tr><td>G-1006</td><td></td><td>8309-23955-4082</td>
    <tr><td>G-1007</td><td></td><td>8338-23955-4053</td>


    </table>

    <script>
    function myFunction() {
    var input, filter, table, tr, td, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");
    for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
    txtValue = td.textContent || td.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
    tr[i].style.display = "";
    } else {
    tr[i].style.display = "none";
    }
    }
    }
    }
    </script>

    </body>
    </html>

    Many Thanks
    Sdg

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make table appear

    JS is not the same as Java.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make table appear

    Oh sorry, as mentioned I’m a complete newbie. Is this not the right forum then?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make table appear

    This forum is for the Java language.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. jtable showing the table content but not showing the table headers
    By vimivimal in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 10th, 2017, 01:27 PM
  2. adding data in table but the table is in another frame
    By brianskiee in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 13th, 2014, 03:31 PM
  3. Help make addition table from stopping once it reaches 12
    By my21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2013, 07:48 PM
  4. How can I make a JDialog to make main Frame to "sleep"
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: May 11th, 2012, 08:00 AM