More JavaScript help...

And back I come with new needs. Alright, using HTML and JavaScript, I’m trying to figure out the following:

[ul]
[li]How to allow users to input data and submit it, but only if everything is filled out
[/li][li]Allow users to create an addiotional input form/variable (say they start with only 4 boxes but need 5 or 6)
[/li][li]Do operations and random number generation using the given information
[/li][li]Be able to refer to values and look them up (i.e. they select items A and B in two drop-down menus, A has a value of 3 and B a value of 4, add them together in the results page)
[/li][li]Make a results page in a pre-defined format
[/li][/ul]

I tried looking on-line, but I don’t really get the stuff they show me. I work best when others help me with it, so sorry if I’m a nuisance.

EDIT: Using my previous page, I’m assuming I can just edit the info there and names and values and such in the first part, but do I need to do the if (myString == X) document.form1.etc… if they’re being selected from a drop-down menu, or is it possible to just do document.form1.etc… and have it just refer to the item listed?

Oof… that’s some major stuff there. I can’t help you with a quick fix on this. You’re really going to have to take out a book on JavaScript and start from scratch so you really understand well what you’re doing. -_- I will give an overview of what you’ll need to do:

Number 1 is contingent on number 5. Basically, instead of a submit button, use a regular button with “onClick=‘checkSubmit(this.form)’”. The checkSubmit(aForm) function would then check that each fields is filled in (tedious), and if so, call aForm.submit().
Number 2 will require DOM, an “add-on” to JavaScript that allows you to add HTML and form elements. It’s quite annoying to work with, though.
3 will require the parseInt() method, which turns a string into an integer. All form elements’ values are treated as strings by default. Once you have the integers it’s a breeze to work with them.
4 will require the Math.random() method.
5 is pretty easy once you know how to refer to fields’ values. E.g. a text box’s value will be document.(form name).(text box name).value. Combo fields are
document.(form name).(combo name).options[document.(form name).(combo name).selectedIndex].value
If you want to stay using JavaScript, number 6 will require using HTTP GET. Basically, you’ll want to do this after submittal:

document.location.href=“result.html?option1=Y&option2=N&myname=Cala…”

And in “result.html” you will have to parse the URL and extract the names and values of your variables. It’s not a trivial problem, unfortunately.

I know most of that didn’t make sense, but like I said, you’re really going to have to know what you’re doing to do all this… it’s fairly powerful for JavaScript and it’s not simple.

Well, to start, could you give me an example code of having two drop-down menus with 2 options each? Editing my current page gets me this:

<html><head><title>Character Generator v1.0</title> 
<script LANGUAGE="JavaScript"> 
function changeStuff() 
{ myString = document.form1.comboSword.options[document.form1.comboSword.selectedIndex].value; 
if (myString == "Damascus Sword") 
{ document.form1.dama.value = "Damascus Sword"; // Sword 
document.form1.dicedama.value = "4d8 + 3xAGI"; // Damage 
} 
else if (myString == "Ultima Sword") 
{ document.form1.ultimas.value = "Ultima Sword" // Sword 
document.form1.diceultimas.value = "5d8 + 6xAGI" // Damage 
} 

function changeStuff() 
{ myString = document.form1.comboBow.options 
[document.form1.comboBow.selectedIndex].value; 
if (myString == "Crossbow") 
{ document.form1.cross.value = "Crossbow"; // Bow 
document.form1.dicecross.value = "3d6 + 4xAGI"; // Damage 
} 
else if (myString == "Long Bow") 
{ document.form1.long.value = "Longbow" // Bow; 
document.form1.dicelong.value = "5d6 + 8xAGI"; // Damage 
} 

} 
</script> 
</head> 
<body> 

<form name="form1"> 
<select name="comboSword" onChange="changeStuff()"> 
<option name="Damascus Sword">Damascus Sword</option> 
<option name="Ultima Sword">Ultima Sword</option> 
</select> 

<select name="comboBow onChange="changeStuff()"> 
<option name="Crossbow">Crossbow</option> 
<option name="Long Bow">Long Bow>/option> 
</select> 
</form> 

</body> 
</html> 

The “3xAGI” part would reference a value given by the user and multiply the two together for the result, but I’m worried more about the first page right now…

I’m not sure what you’re asking regarding that code… what would you like to happen exactly?

For what I had before, but with mutiple drop-down menus in use, with each option having a different value.

Looks okay to me, then, just close the quotes after the NAME in “comboBow”.

How do I refer to an user given number in a form box? So if I named the input box “die1” or somesuch, I can get the number they input there for reference and use in math.

Assuming the form is NAMEd “form1”, like so: document.form1.die1.value

And if you want to turn it into an integer (number) rather than a string, use
parseInt(document.form1.die1.value).

Alright, time to do a lot of copying and pasting. I’ll post the finished page with the input stuff sometime soon…

A question, though: Do I need to change “function changeStuff()” for every drop-down menu I use?

EDIT: Also, is it possible to change what other drop-down menus say based on a previous choice? Say if they choose “Expert” from one drop-down menu, the next would list several options, but if they chose “Warrior” it would show different options. And it would also affect some math operations too.

You can use changeStuff() to deal with every combo box if you like, but it’d make more sense to make a separate function for each combo box.
And yes, it’s possible to do those other things. You’d have to do something like this:
if (myString == “Expert”)
{document.form1.box2.options[0].value = “NewValue1”;
document.form1.box2.options[1].value = “NewValue2”;
etc…
}

I currently have this…

<html><head><title>Character Generator v1.0</title> 
<script LANGUAGE="JavaScript"> 
function changeStuff() 
{ myString = document.form1.comboClass.options[document.form1.comboClass.selectedIndex].value; 
if (myString == "Warrior")
{ document.form1.class.value = "Warrior"; // Warrior
}
else if (myString == "Mage")
{ document.form1.class.value = "Mage"; // Mage
}
else if (myString == "Expert")
{ document.form1.class.value = "Expert"; // Expert
}
else if (myString == "Adept")
{ document.form1.class.value = "Adept"; // Adept
}
}

if(document.form1.comboClass.value = "Warrior")
{
function changeStuff()
{ myString = document.form1.comboJob.options[document.form1.comboJob.selectedIndex].value; 
if (myString == "Archer")
{ document.form1.job.value = "Archer"; // Archer
}
else if (myString == "Dragoon")
{ document.form1.job.value = "Dragoon"; // Dragoon
}
}
}

else if(document.form1.comboClass.value = "Mage")
{
function changeStuff()
{ myString = document.form1.comboJob.options[document.form1.comboJob.selectedIndex].value; 
if (myString == "White Mage")
{ document.form1.job.value = "White Mage"; // White Mage
}
else if (myString == "Black Mage")
{ document.form1.job.value = "Black Mage"; // Black Mage
}
}
}

else if(document.form1.comboClass.value = "Expert")
{
function changeStuff()
{ myString = document.form1.comboJob.options[document.form1.comboJob.selectedIndex].value; 
if (myString == "Dancer")
{ document.form1.job.value = "Dancer"; // Dancer
}
else if (myString == "Mime")
{ document.form1.job.value = "Mime"; // Mime
}
}
}

else if(document.form1.comboClass.value = "Adept")
{
function changeStuff()
{ myString = document.form1.comboJob.options[document.form1.comboJob.selectedIndex].value; 
if (myString == "White Caller")
{ document.form1.job.value = "White Caller"; // White Caller
}
else if (myString == "Black Caller")
{ document.form1.job.value = "Black Caller"; // Black Caller
}
}
}
}
</script> 
</head> 
<body> 

<input type="text" name="charaname"><br>
<br>
<form name="form1"> 
<select name="comboClass">
<option value="Warrior">Warrior</option>
<option value="Mage">Mage</option>
<option value="Expert">Expert</option>
<option value="Adept">Adept</option>
</select><br>
</form> 

</body> 
</html>

The question now is how do I set up the second drop-down menu?

Guh… I honestly don’t mind helping you, but I’m a bit busy and this is gonna take some time. ^^; And if you really want to do all the stuff you mentioned up top, you’re going to need a better grasp of HTML forms and JavaScript. I really recommend you just take a For Dummies™ book out of the library and read through it… you’ll find it’s easier to do what you want yourself rather than keeping asking me about it. :sunglasses:

I don’t mind waiting to get help. I’ve tried the books, though; horrible experience for me. I learn best when I get help from others. I’m also getting help from a JS teacher and a kid pretty fluent with it, so between the three of you, it’s shaping up pretty fast, although you’re helping me fastest.

OK, then… let’s see. I think you’re confused about functions. A function is essentially an order to give. So if I put <SELECT onChange=“changeStuff()”> in my HTML page, that means "find the function “changeStuff()” and execute it, whenever this combo box is changed.

Now, you can’t have more than one function with the same name, because then the system won’t know which one you mean. So you’ll have to give them different names.

Also, function declarations (i.e. “function changeStuff()”) can’t be inside another function. That also doesn’t make much sense.

One last thing: Variables declared inside a function can’t be used in another function. So if you make “myString” in your first function, you can’t use it again in the next one, unless you pass it as an argument (which we’re not doing, nor do we really need to right now).

Here’s the code which does what I think you’re trying to do:

<html><head><title>Character Generator v1.0</title>
<script LANGUAGE="JavaScript">
function changeStuff()
{   var myClass = document.form1.comboClass.options[document.form1.comboClass.selectedIndex].value;
	if (myClass == "Warrior")
	{ document.form1.comboJob.options[0] = new Option("Archer", "Archer");
	  document.form1.comboJob.options[1] = new Option("Dragoon", "Dragoon");
	}
	else if (myClass == "Mage")
	{ 
	  document.form1.comboJob.options[0] = new Option("White Mage", "White Mage");
	  document.form1.comboJob.options[1] = new Option("Black Mage", "Black Mage");
	}
	else if (myClass == "Expert")
	{ document.form1.comboJob.options[0] = new Option("Dancer", "Dancer");
	  document.form1.comboJob.options[1] = new Option("Mime", "Mime");
	}
	else if (myClass == "Adept")
	{ document.form1.comboJob.options[0] = new Option("White Caller", "White Caller");
	  document.form1.comboJob.options[1] = new Option("Black Caller", "Black Caller");
	}
}

</script>
</head>
<body>

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

<form name="form1">
<select name="comboClass" onChange="changeStuff()">
<option value="Warrior">Warrior</option>
<option value="Mage">Mage</option>
<option value="Expert">Expert</option>
<option value="Adept">Adept</option>
</select>
<P><SELECT NAME="comboJob">
</SELECT>
</form>

</body>
</html>  

Exactly what I’m looking for, thanks.

EDIT: I assume to have the second/third (the specific Job lists) have values, I just add another function like I already have?

EDIT: Ah shit, I broke it. http://cala.nulani.net/generator.html

I get this error whenever I choose ANY drop-down menu right now.

Error: document.formweap5 has no properties
Source File: file://///XAV-student-9/006080$/My%20Documents/generator.html
Line: 342

I’ll upload the file I’m using in a few hours, as I don’t have access to it right now. Also, the Class affecting Job drop-down menu has stopped working, and it gives me no errors.

Show me the code and I’ll fix it for you.

http://cala.nulani.net/generator.html

Hey Cid, you got a messenger or in the chat? It would be faster at times when we’re both here.

Unfortunately, no and no. I did have ICQ before it started slowing down my computer, and I’m not usually interested in random people chatting with me; I check my e-mail very often and that’s the best way to get ahold of me.

Anyhoo… don’t forget that you’re dealing with document.(form name).(field name).value. The form name has to actually exist, i.e. the field must be inside a <FORM NAME=“formName”> tag. I’d tell you to just have a single form name, which should make things easier. And the field name, such as <INPUT type=“text” name=“fieldName”> also has to exist inside that form.
Also, don’t forget to close your quotations for tag attributes. And don’t forget to have a closing </SELECT> after your last OPTION.

Here’s the fixed code:
http://www.rpgclassics.com/staff/cidolfas/temp.html

And it broke again.

Error: document.form1.skill01 has no properties
Source File: file://///XAV-student-9/006080$/My%20Documents/generator.html
Line: 401

Relevant code:

function changeSkill01()
{   var mySkill01 = document.form1.comboSkill01.options[document.form1.comboSkill01.selectedIndex].value;
if (mySkill01 == " ")
{ document.form1.skill01.value = null;
}
else if (mySkill01 == "Acrobatics")
{ document.form1.skill01.value = "Acrobatics";
}
else if (mySkill01 == "Acting")
{ document.form1.skill01.value = "Acting";
}
else if (mySkill01 == "Alchemy")
{ document.form1.skill01.value = "Alchemy";
}
else if (mySkill01 == "Animal Training")
{ document.form1.skill01.value = "Animal Training";
}
else if (mySkill01 == "Art")
{ document.form1.skill01.value = "Art";
}
else if (mySkill01 == "Awareness")
{ document.form1.skill01.value = "Awareness";
}
else if (mySkill01 == "Climbing")
{ document.form1.skill01.value = "Climbing";
}
else if (mySkill01 == "Command")
{ document.form1.skill01.value = "Command";
}
else if (mySkill01 == "Concealment")
{ document.form1.skill01.value = "Concealment";
}
else if (mySkill01 == "Cooking")
{ document.form1.skill01.value = "Cooking";
}
else if (mySkill01 == "Crafting")
{ document.form1.skill01.value = "Crafting";
}
else if (mySkill01 == "Dancing")
{ document.form1.skill01.value = "Dancing";
}
else if (mySkill01 == "Disguise")
{ document.form1.skill01.value = "Disguise";
}
else if (mySkill01 == "Escape")
{ document.form1.skill01.value = "Escape";
}
else if (mySkill01 == "Etiquette")
{ document.form1.skill01.value = "Etiquette";
}
else if (mySkill01 == "Explosives")
{ document.form1.skill01.value = "Explosives";
}
else if (mySkill01 == "Gambling")
{ document.form1.skill01.value = "Gambling";
}
else if (mySkill01 == "Healing")
{ document.form1.skill01.value = "Healing";
}
else if (mySkill01 == "Inquiry")
{ document.form1.skill01.value = "Inquiry";
}
else if (mySkill01 == "Instrument")
{ document.form1.skill01.value = "Instrument";
}
else if (mySkill01 == "Intimidation")
{ document.form1.skill01.value = "Intimdation";
}
else if (mySkill01 == "Invent")
{ document.form1.skill01.value = "Invent";
}
else if (mySkill01 == "Language")
{ document.form1.skill01.value = "Language";
}
else if (mySkill01 == "Lockpicking")
{ document.form1.skill01.value = "Lockpicking";
}
else if (mySkill01 == "Lore")
{ document.form1.skill01.value = "Lore";
}
else if (mySkill01 == "Navigation")
{ document.form1.skill01.value = "Navigation";
}
else if (mySkill01 == "Negotiation")
{ document.form1.skill01.value = "Negotiation";
}
else if (mySkill01 == "Perception")
{ document.form1.skill01.value = "Perception";
}
else if (mySkill01 == "Pickpocket")
{ document.form1.skill01.value = "Pickpocket";
}
else if (mySkill01 == "Poach")
{ document.form1.skill01.value = "Poach";
}
else if (mySkill01 == "Repair")
{ document.form1.skill01.value = "Repair";
}
else if (mySkill01 == "Riding")
{ document.form1.skill01.value = "Riding";
}
else if (mySkill01 == "Seduction")
{ document.form1.skill01.value = "Seduction";
}
else if (mySkill01 == "Singing")
{ document.form1.skill01.value = "Singing";
}
else if (mySkill01 == "Stealth")
{ document.form1.skill01.value = "Stealth";
}
else if (mySkill01 == "Sreetwise")
{ document.form1.skill01.value = "Streetwise";
}
else if (mySkill01 == "Survival")
{ document.form1.skill01.value = "Survival";
}
else if (mySkill01 == "Swimming")
{ document.form1.skill01.value = "Swimming";
}
else if (mySkill01 == "Systems")
{ document.form1.skill01.value = "Systems";
}
else if (mySkill01 == "Teaching")
{ document.form1.skill01.value = "Teaching";
}
else if (mySkill01 == "Tracking")
{ document.form1.skill01.value = "Tracking";
}
else if (mySkill01 == "Trade")
{ document.form1.skill01.value = "Trade";
}
else if (mySkill01 == "Traps")
{ document.form1.skill01.value = "Traps";
}
else if (mySkill01 == "Vehicles")
{ document.form1.skill01.value = "Vehicles";
}

}

And in the body…

<tr align="center"><td>
<select name="comboSkill01" onChange="changeSkill01()">
<option value=" "></option>
<option value="Acrobatics">Acrobatics</option>
<option value="Acting">Acting</option>
<option value="Alchemy">Alchemy</option>
<option value="Animal Training">Animal Training</option>
<option value="Art">Art</option>
<option value="Awareness">Awareness</option>
<option value="Climbing">Climbing</option>
<option value="Command">Command</option>
<option value="Concealment">Concealment</option>
<option value="Cooking">Cooking</option>
<option value="Crafting">Crafting</option>
<option value="Dancing">Dancing</option>
<option value="Disguise">Disguise</option>
<option value="Escape">Escape</option>
<option value="Etiqutte">Etiquette</option>
<option value="Explosives">Explosives</option>
<option value="Gambling">Gambling</option>
<option value="Healing">Healing</option>
<option value="Inquiry">Inquiry</option>
<option value="Instrument">Instrument</option>
<option value="Intimidation">Intimidation</option>
<option value="Invent">Invent</option>
<option value="Language">Language</option>
<option value="Lockpicking">Lockpicking</option>
<option value="Lore">Lore</option>
<option value="Navigation">Navigation</option>
<option value="Negotiation">Negotiation</option>
<option value="Perception">Perception</option>
<option value="Pickpocket">Pickpocket</option>
<option value="Poach">Poach</option>
<option value="Repair">Repair</option>
<option value="Riding">Riding</option>
<option value="Seduction">Seduction</option>
<option value="Singing">Singing</option>
<option value="Stealth">Stealth</option>
<option value="Streetwise">Streetwise</option>
<option value="Survival">Survival</option>
<option value="Swimming">Swimming</option>
<option value="Systems">Systems</option>
<option value="Teaching">Teaching</option>
<option value="Tracking">Tracking</option>
<option value="Trade">Trade</option>
<option value="Traps">Traps</option>
<option value="Vehicles">Vehicles</option>
</select></td><td><input type="text" name="point01"></td></tr>