Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    please help me. i want to limit it (javascript)

    i have this txt file name contacts.txt that contains this:

    kate|female|kathryn bailey beckinsale|26-jul-1973|#23 underworld drive|(621) 142-7827|kate@lycans.net
    jessica|female|jessica claire biel|03-mar-1982|27 texas avenue|(53)2344223|jbiel@yahoo.com
    johnny|male|john christopher depp ii|09-jun-1963|711 pirate road|(773) 476-6634|jsparrow@piratebay.org


    my script is this:

    function syncText() {
    var xhr = new XMLHttpRequest();

    xhr.open("get", "data/contacts.txt", false);

    xhr.send(null);

    if (xhr.status == 200) {
    var data = xhr.responseText;
    var items = data.split("|");
    items.sort();

    var div = document.getElementById("header2");
    for (var i = 0; i < items.length; i++) {
    var p = document.createElement("p");
    var text = document.createTextNode(items[i]);
    p.appendChild(text);
    div.appendChild(p);
    }
    } else {
    alert("data retrieval failed...");
    }
    }


    html

    <div id="header2">
    <button onclick="syncText()">Pak</button>
    </div>

    i only want to retreive kat, jessica, johnny.. please help me

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    You should be splitting on \n first to load an array with each line in a separate entry - then split each of those on | and discard all but the [0] entry of each sub-array
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how to use the /n ? it does not wortk...

    var items = data.split("/n");
    items = data.split("|",1);
    Last edited by RakiZtahX; 02-09-2013 at 01:58 AM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    \n is a linefeed

    /n is a slash followed by an n

    you typed the wrong character
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah im sorry... but the output is this.. and i want only to get the angie, and anne (names only)

    angie|female|angelina jolie voight|04-jun-1975|#43 hollywood blvd|(773) 487-4534|laracroft@wanted.com

    anne|female|anne jacqueline hathaway|12-nov-1982|#3c royal avenue|(99) 452-4443|anne@diaries.com

    please help me

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function syncText() {
    var xhr = new XMLHttpRequest();

    xhr.open("get", "data/contacts.txt", false);

    xhr.send(null);

    if (xhr.status == 200) {
    var data = xhr.responseText;
    var items = data.split("\n");
    items.sort();
    var div = document.getElementById("header2");


    for (var i=0;i <items.length; i++) {
    var names=[];
    names.push(items[i].split("|")[0] );
    var p = document.createElement("p");
    var text = document.createTextNode(items[names]);
    p.appendChild(text);
    div.appendChild(p);
    }
    } else {
    alert("data retrieval failed...");
    }
    }


    i have this codes now.. but it's undefined... what should i do?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •