jQuery Loop Through Read Text File

I already discussed how to get individual record of a text database in previous post. This method is good only if we know the element number of the searched record. In most cases, computer programming would like to loop through the whole database searching for the required records and data. Let’s see how to do that.

<head>
<title>jQuery Loop Through Read Text File</title>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $("document").ready(function() {
        
         $.get('http://localhost/xampp/jphp/urlData.txt', function(data) {
                    
                    // The result (data) now passed back is a whole trunk of data
                    // alert(data);    

                    // Split the whole database trunk into individual lines or records
                    // The record delimiter is carriage return. Therefore split it.
                    var lines = data.split("\n");
            
                    // Loop through all lines of record
                    $.each(lines, function(n, urlRecord) {
                         $('#simpleDiv').append('<div>' + urlRecord + '</div>');    
                    });
            });
            
    });
</script>

</head>
<body>

<h2>jQuery Loop Through Read text File </h2>

<div id="simpleDiv"></div>

</body>
</html>

The use of jQuery each() method is simply to loop through the callback function.

The result is shown as below:

jQuery example File:

Click here to download or view the file.

After knowing how to use jQuery loop through the read text database line by line, this is up to you how to use the records and data.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>