jQuery Manipulate Read Text File

I got the text database file with jQuery.get() method successfully in my previous post. Like most computer application, the next step is see how to manipulate the source database.

Let’s review the structure of this simple text database (urlData.txt):


This is obvious that each line is a record. Each record has only a data (url). In other words, there are total three records in urlData.txt text database. Of course, each record can have more than one data, with each data separate with delimiter, for example:




The overall concept is to get the database, get the required record, and finally get the required data.

Okay! Let’s see how to handle the passed back text database.

This is quite obvious that the first step after getting the text database is to see how to get each record.

In order to get each record, we have to split the whole trunk of text database into individual record (or line). And then getting each record. This is as shown in the example below:

<title>jQuery Manipulate 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");

                    // Test if we can get the record with this method
                    alert ("The first record is: "  + lines[0]);
                    alert ("The second record is: " + lines[1]);



<h2>jQuery Manipulate Read text File</h2>


The results will be shown as below when open the web page:

Here’s the procedures:

  • Each record of the text database is linked by the carriage return (“\n”). Therefore we need to split the carriage return that linked the the whole truck of record.
  • After splitting the whole trunk of records into individual records or lines. We can get the individual records by specifying the corresponding element number. The first record has element number 0, the second record has element number 1, etc…

The number of lines or records is: lines.length

jQuery example File:

Click here to download or view the file.

Actually this method of getting individual record is almost the same as other computer programming.

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>