Greenguy's Board


Go Back   Greenguy's Board > Programming & Scripting
Register FAQ Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 2006-11-24, 12:53 PM   #1
Useless
Certified Nice Person
 
Useless's Avatar
 
Join Date: Oct 2003
Location: Dirty Undies, NY
Posts: 11,268
Send a message via ICQ to Useless
Javascript help

So I'm working with some borrowed JS and I haven't a clue of what I'm doing with it. Without going way too deeply into the project, let's say that I have a nice little HTML form with three <input> boxes which are named consecutivley "word", "word2" and "word3". When I click on a list of keywords, the first word I click gets inserted in to box "word" by the JS. When I click a second word selection it gets inserted into "word2". What I'd like to do is add to the following JS so I can insert the next word selection into "word3" - but I don't know how to. Please help.


function addWord(keyword) {
if (document.forms[0].word.value == "") {
document.forms[0].word.value = keyword;
} else {
document.forms[0].word2.value += keyword;
}
}

Thanks folks.
Useless is offline   Reply With Quote
Old 2006-11-24, 01:27 PM   #2
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
basically, that

document.forms[0].word2.value

replaces

<input type="text" name="word2">

with whatever you assign it.

If I am assuming what you want is true, you could do something like:

function addWord(keyword) {
if (document.forms[0].word.value == "") {
document.forms[0].word.value = keyword;
} else {
if (document.forms[0].word2.value == "") {
document.forms[0].word2.value = keyword;
} else {
document.forms[0].word3.value = keyword;
}
}
}

You had a += in there before, which would have appended whatever was selected to the 2nd field. If the 2nd field had 'asdf' in it, you would have had 'asdfwhatever' rather than it setting the field with 'whatever' the conditions would have also broken if field2 had a value, and with the construct above, would set field 1 and field 3.

However, a better way to do this might be to keep track of a counter and increment after each is set -- otherwise when you add a 4th field, you're just going to get an unmanageable mess of nested ifs.
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote
Old 2006-11-24, 02:34 PM   #3
Useless
Certified Nice Person
 
Useless's Avatar
 
Join Date: Oct 2003
Location: Dirty Undies, NY
Posts: 11,268
Send a message via ICQ to Useless
Thank you cd34. It works!

I played my normal copy/paste/delete game with those few lines of code for quite awhile last night and never figured it out. Now I get to go and add that third field into my script and another row into a mySQL table. Yay.
Useless is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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 -4. The time now is 12:25 PM.


Mark Read
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc