Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2013, 03:34 PM   PM User | #1
RakiZtahX
New to the CF scene

 
Join Date: Feb 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
RakiZtahX is an unknown quantity at this point
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
RakiZtahX is offline   Reply With Quote
Old 02-08-2013, 08:07 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is online now   Reply With Quote
Old 02-09-2013, 01:36 AM   PM User | #3
RakiZtahX
New to the CF scene

 
Join Date: Feb 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
RakiZtahX is an unknown quantity at this point
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..
RakiZtahX is offline   Reply With Quote
Old 02-09-2013, 03:08 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
\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/
felgall is online now   Reply With Quote
Old 02-09-2013, 04:12 AM   PM User | #5
RakiZtahX
New to the CF scene

 
Join Date: Feb 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
RakiZtahX is an unknown quantity at this point
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
RakiZtahX is offline   Reply With Quote
Old 02-09-2013, 05:10 AM   PM User | #6
RakiZtahX
New to the CF scene

 
Join Date: Feb 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
RakiZtahX is an unknown quantity at this point
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?
RakiZtahX is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:59 PM.


Advertisement
Log in to turn off these ads.