![]() |
Javascript help
So I'm working with some borrowed JS and I haven't a clue of what I'm doing with it.:D Without going way too deeply into the project, let's say that I have a nice little HTML form with three 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. |cry|
function addWord(keyword) { if (document.forms[0].word.value == "") { document.forms[0].word.value = keyword; } else { document.forms[0].word2.value += keyword; } } Thanks folks. |
basically, that
document.forms[0].word2.value replaces 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. |
Thank you cd34. It works!|headbang|
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. :D |
All times are GMT -4. The time now is 01:16 PM. |
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc