Click to See Complete Forum and Search --> : a little javascript problem


Da Warriah
12-03-2002, 01:56 PM
ok, this is my first post here, hey everyone...

this shouldnt be too hard i dont think, i just cant seem to come up with any ideas, probly use something like charAt() or indexOf()...anyways, here we go:

im working on a script that will be used for news, and i have a page with a few textboxes and textareas and stuff, one being for the title of the news...what i want is for this script to take the value from the title textbox and change every letter into an <img> tag with a picture of the letter in a cool font...but how do i make the script decide which image needs to go with which letter of the alphabet and such? itll be something like this process (not in javascript form):

if letter is "a", then replace with <img src="a.gif">;
if letter is "b", then replace with <img src="b.gif">;
and so on...

AdamGundry
12-03-2002, 03:58 PM
Try something like this (this assumes you have a variable "titletext" containing the title:


var i = 0;

for (i=0;i<titletext.length;i++){
document.write('<img src="' + titletext.charAt(i) + '">');
}


Document.write() will only work if you are calling it on the first rendering of the page; otherwise, you will probably need to assemble some code inside a <DIV> using divname.innerHTML.

Hope this helps

Adam

Da Warriah
12-03-2002, 04:49 PM
k....i dont think that quite would work in my situation...but its given me an idea...

lets give u ppl an example:

i have some news i want to put in, so i put the title "The News" in my "title" textbox...now the script takes that title, converts each letter into its corresponding image (<img src="t.gif"><img src="h.gif"> etc), puts it into a "hidden" input, and then the php script will take that and post it no problem, etc etc...

now, would i be able to use that loop still to do it like that? like say make it like:

var i=0

for (i=0;i<title.length;i++){
document.form.title.value="<img src=\""+title.charAt(i)+".jpg\">";
}

for one thing, would that work? and if so, how would i make it so it added it onto the end of the value instead of replacing it??

grrrr...i wish i werent so rusty with JavaScript...

AdamGundry
12-04-2002, 11:05 AM
I think you're almost there. Assuming you have a textbox called "titletext" in which you enter the title, and it is put into a hidden field called "hiddentext" as <IMG> tags (both elements are in a form called "form"):


var i = 0;

for (i=0;i<document.form.titletext.value.length;i++){
document.form.hiddentext.value = document.form.hiddentext.value + '<img src="' + titletext.charAt(i) + '.gif">';
}



This should generate the entire title and put it in the hidden field. In order to add on to the end of the string, you simply use string = string + addition. It might be more efficient to create a temporary string to hold the hiddentext field's value, instead of reading/writing it each time.

Good luck

Adam