Forum Moderators: open

Message Too Old, No Replies

Javascript to populate textbox: not working

         

Maceman

10:01 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Can someone please have a look at this and let me know what i am missing and why the text box doesn't populate with the file "Man.txt"

I found this code on the web and manipulated it to my use, but neither the original nor my changes work....

I just want to get the txt to populate and i can figure out how to do the rest for multiple lyrics.
Thanx in advance!

Maceman

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<script type="text/javascript">function put_link(){ document.getElementById('chosen_link').value = document.getElementById('lyrics').value;}</script>
</head>
<body>
<select id="lyrics">
<option value="http://www.mywebsitehere.com/lyrics/Man.txt">Lyric1</option>
<option value="http://link">Lyric2</option>
<option value="http://link">Lyric3</option>
</select>
<button onclick="put_value();">Get Lyrics</button>
<input type="text" id="chosen_link"/>
</body>
</html>

Little_G

10:50 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Hi,

Your function is called put_link but your calling put_value.

Andrew

Maceman

11:58 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Hi Andrew,

Thanx for that. Ok...So I change it to put_link, but all that happened was that the URL pasted into the text box. Do you know how to make the actual contents of the text file go in instead?
Thanx,
Maceman

Little_G

10:41 am on Oct 12, 2008 (gmt 0)

10+ Year Member



Hi,

You'll have to write script that will download the document and then put the contents in the text box.
Take a look at Mozilla's introduction to AJAX [developer.mozilla.org].

Andrew

Maceman

3:01 pm on Oct 14, 2008 (gmt 0)

10+ Year Member



Hi Andrew,

Pweew, I read the whole thing, but must admit that it is over my head.
I think i learn best by example, that is, if someone made a script using examples, i could 'tweak' it to work for my purposes. I know that may sound lame, but i cannot even think to begin on how to make the code properly as I know nothing about AJAX, however, it does sound exactly what i want to use. It seems to me that AJAX is what is used for these MP3 jukeboxes that are populated from an XML Playlist? Seems similar to what I am trying to achieve, but without music in a directory, but rather just text.

Maceman

Little_G

6:28 pm on Oct 14, 2008 (gmt 0)

10+ Year Member



Hi,

Here's an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<script type="text/javascript">
function put_file_contents(url, output){
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
output.value = httpRequest.responseText;
}
else {
output.value = "error";
}
}
else {
output.value = "Downloading...";
}
};
httpRequest.open('GET', url, true);
httpRequest.send('');
}
</script>
</head>
<body>
<select id="lyrics">
<option value="http://www.mywebsitehere.com/lyrics/Man.txt">Lyric1</option>
<option value="http://link">Lyric2</option>
<option value="http://link">Lyric3</option>
</select>
<button onclick="put_file_contents(document.getElementById('lyrics').value,
document.getElementById('chosen_link'));">Get Lyrics</button>
<input type="text" id="chosen_link"/>
</body>
</html>

Andrew

[edited by: Little_G at 6:31 pm (utc) on Oct. 14, 2008]

[edited by: DrDoc at 5:07 pm (utc) on Oct. 20, 2008]
[edit reason] fixed side scrolling [/edit]

Maceman

7:26 pm on Oct 14, 2008 (gmt 0)

10+ Year Member



Wow. That is fantastic! Thanx so much.....

Ok, last question on this. Is there a way to maintain the format of the original file, that is, is there a way that when the text is imported that it maintains the original look, with all the formatting etc...because what happens is that when it populates now, it becomes one long sentence....
Alternatively, if there was a code to add in here that would standardize the 'look' of what is imported, so that it fills the box, rather than one sentence outside the box..... Does this make sense?

I made the text box length and width bigger, but the imported text still wants to stay in a sinbgle sentence without word wrapping or satying formatted....

This is great and you are the first person I have seen anywhere to get this working for me, and i have browsed a number of threads by googling!

Maceman
ps. Here is what the text stadardization would look like:

Bolded and Centred: Lyric Title
Bolded and centred:© 2008
centred: Old Macdonald had a farm
centred: E I E I O
centred: And on his farm he had some cows
centred: E I E I O

-sorry, i couldn't think of another sample song :)

Maceman

7:46 pm on Oct 14, 2008 (gmt 0)

10+ Year Member



^^further to my last post, i was able to resize the text box and it is much better, that is, with a wider width and height the lyrics appear better and are word wrapping, but the same issue is still there becasue each file that will populate will of course have different attributes as for width etc...so in retrospect, I am thinking using a standardized approach wouldn't necessarily be what i want, but rather if the original could be maintained, in which case i can simply make sure I foramt each file appropriately beforehand.....
But I will await your advice in the matter, and I am simply thinking out loud!
Maceman

Maceman

8:52 pm on Oct 14, 2008 (gmt 0)

10+ Year Member



^^I also notice that this isn't working for me in FF. The first lyric loads, but the rest do ,not, i have 2 now in total and they work in IE.... Thanx.

Maceman

8:54 pm on Oct 14, 2008 (gmt 0)

10+ Year Member



Ok, it works in FF- I didn't upload the CSS... Ahhhhhh... Ok, I am going to bed....{bangs head on wall}

Little_G

9:29 am on Oct 15, 2008 (gmt 0)

10+ Year Member



Hi,

If you are loading a multi-line file then chosen_link would be better off as a textarea [w3.org].

Andrew

Maceman

7:07 pm on Oct 15, 2008 (gmt 0)

10+ Year Member



Ok, changes the instances of chosen_link to textarea. Seems to work pretty much the same so far.
How can i get the textarea to do this now:

Bolded and Centred: Lyric Title
Bolded and centred:© 2008
centred: Old Macdonald had a farm
centred: E I E I O
centred: And on his farm he had some cows
centred: E I E I O

<button onclick="put_file_contents(document.getElementById('lyrics').value, document.getElementById('textarea'));">
<p align="center"><strong>Get Lyrics</strong></p>
</button>
<textarea name="textarea" cols="20" rows="10" wrap="physical" class="box" id="textarea" style="WIDTH: 300px; HEIGHT: 300px"></textarea></td>
</tr>

Thanx again!
Maceman

Little_G

4:33 pm on Oct 16, 2008 (gmt 0)

10+ Year Member



Hi,

How is this formatting saved in the files?

Andrew

Maceman

12:39 pm on Oct 17, 2008 (gmt 0)

10+ Year Member



Well, I tried saving as a txt which gets rid of formatting, which is no good, and then I tried an rtf which maintains the formatting, but then all the rtf code comes in instead of the formatted text. So nothing I have tried works unfortunately.
The import works great for straight, non-formatted text, but that is bland for what I am wanting.
Any help is appreciated.
Maceman

Little_G

10:51 am on Oct 18, 2008 (gmt 0)

10+ Year Member



Hi,

You could save the files with html formatting and output the contents to a div.
So,
HTML:

<button onclick="put_file_contents(document.getElementById('lyrics').value, 
document.getElementById('output'));">
<p align="center"><strong>Get Lyrics</strong></p>
</button>
<div id="output" style="width: 300px; height: 300px"></div>

Javascript:

if (httpRequest.status == 200) {  
output.innerHTML = httpRequest.responseText;
}

Lyrics File:

<h2>Lyric Title</h2> 
<h3>© 2008</h3>
<p>Old Macdonald had a farm<br>
E I E I O<br>
And on his farm he had some cows<br>
E I E I O</p>

Andrew

[edited by: DrDoc at 5:07 pm (utc) on Oct. 20, 2008]
[edit reason] fixed side scrolling [/edit]

Maceman

12:26 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



That's strange. I left a response last night and came to see if there was a response and my response was gone? Weird...

Anyways, what I had mentioned was that I tried the above code, and what happens if that when i select the lyric from the dropdown menu that has a reference to an html file, what happens is that a new apperance of the dropdown menu and "get Lyrics" button appears below the first set.
I looked in the code to see if I had somehow duplicated something, but I don't see anything of hand. What could cause that? So in short, the text box populates with another dropdown menu and button, rather than the HTML :)

Thanx again!
Maceman

Little_G

10:28 am on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi,

It sounds like the html file you are referencing has a drop down menu and "get Lyrics" button in it.

Andrew

Maceman

12:56 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi Andrew,

That was exactly the problem! What a dope I am....
Ok, this is working fabulous. Ijust need to do some layout adjusting and such, but it is calling the html perfectly and is retaining the format! Thanx so much!
Maceman

Maceman

2:16 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



One last thing...{I've said that before haven't I :)}

Is there a way to make it so when the text box is populated by the html that the text box sizes to the html size? I had one set of lyrics that was kinda wide so i set my width bigger, and everything works well, but for the smaller sized lyrics, i got all this room around. Is there an auto command or something?

Here is my code:

<td width="420" valign="bottom"></embed>
<select id="lyrics" style="WIDTH: 150px" valign="top">

and then...

div id="output" style="WIDTH: 524px; HEIGHT: 300px"></div></td></tr></table></div>

Thanx,
Maceman

Little_G

4:02 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi,

I don't know of a way to auto-size the div, sorry.

Andrew

Maceman

4:24 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi Andrew,
Ok... I added this overflow into the code and it seems to be working:

<div id="output" style="WIDTH: 524px; HEIGHT: 300px; OVERFLOW: auto;"></div>

What happens however is, if i scroll down the lyrics(so lets say the scroll is halfway down) and then i go to select another lyrics from the dropdown menu, it indeed populates with the correct html, but instead of resetting the scroll to the op, it is still halfway down. I wonder if there is something that would reset that DIV each time?

Maceman
ps. I think i create more problems the more things i try :)

Little_G

4:30 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi,

Take a look at [developer.mozilla.org ].

Andrew

Maceman

5:23 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi Andrew,

I don't get where to put that and how to rename things? Can you give me an example using my code please?

Looks like this is a big issue with many people from what i've seen on several forums, but most require link to rest, i want it to just reset by clicking another selection i nthe drop down...

Thanx,
Maceman

Little_G

6:16 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi,

Javascript:

if (httpRequest.status == 200) {  
output.innerHTML = httpRequest.responseText;
output.scrollTop = 0;
}

Andrew

[edited by: Little_G at 6:17 pm (utc) on Oct. 21, 2008]

Maceman

6:46 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Hi Andrew,

That did it! Beautiful!

Thanx so much!

Maceman

Little_G

7:23 pm on Oct 21, 2008 (gmt 0)

10+ Year Member



Glad I could help.

Andrew