sunflower Posted September 17, 2010 Share Posted September 17, 2010 well I'm back. I seem to be going further in over my head with my name generator script. I'm trying to tell it that if 3 conditions are present on the form to give me random names from a certain array. The fist two conditions were easy.. It's the third condition that I'm having trouble writing. For the third condition I want it to check the first letter of the name in the text input box of the form, see what it is, and then if its an A name, for instance, I will have it to choose names from an array with all A names. This is what i have so far thats not working.. and i know the problems in the third condition cause it works if i take it out.. var firstname = nameform.firstname.value;if((nameform.gender.options[0].selected == true) && (nameform.species.options[0].selected == true)&& (firstname.substring(0) == 'A')){i = Math.floor(Math.random() * malebothanA.length);nameform.output.value=malebothanA[i] + " " + bothanlastname[r]} I wouldnt be surprised if I wrote that all wrong. I just hope someone here can help me cause I dont think I could ever find any js tutorials that explain how to do this in a way that a newbie like me can understand. Link to comment Share on other sites More sharing options...
jeffman Posted September 17, 2010 Share Posted September 17, 2010 You can read about the correct usage of substring here: http://www.w3schools.com/jsref/jsref_substring.aspBasically, the method requires 2 arguments: a starting index, and an ending index.You might be happier with the charAt method: http://www.w3schools.com/jsref/jsref_charat.asp Link to comment Share on other sites More sharing options...
sunflower Posted September 17, 2010 Author Share Posted September 17, 2010 Thankyou Deirdre's dad. I have been reading about charAT. I feel like the problem is in the way i am referencing the form field. I have tried it this way, if((nameform.gender.options[0].selected == true) && (nameform.species.options[0].selected == true)&& (nameform.firstname.value.charAt(0) == 'A')); I have also tried with the word document in front like this, document.nameform.firstname.value.charAT. Neither one is working. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 17, 2010 Share Posted September 17, 2010 First off, you should ditch the dot notation and assign id's to your elements and use document.getElementById()Second, Try alerting the value of your text box, ie:alert(document.getElementById("firstName").value); (EDIT: This is assuming you put an id='firstName' on the appropriate text box)if (....) {...Is it what you expect it to be? Link to comment Share on other sites More sharing options...
jeffman Posted September 17, 2010 Share Posted September 17, 2010 To clarify. Shadow is suggesting a form that is structured kind of like this: <form action="" method="post"> <select id="gender"> <option value="blah">blah</option> <option value="blah">blah</option> <option value="blah">blah</option> </select> <select id="species"> <option value="blah">blah</option> <option value="blah">blah</option> <option value="blah">blah</option> </select> <input type="text" id="firstName"></form> Now you should reliably get data from references like these: document.getElementById("gender").options[0].selecteddocument.getElementById("species").options[0].selecteddocument.getElementById("firstName").value Link to comment Share on other sites More sharing options...
sunflower Posted September 18, 2010 Author Share Posted September 18, 2010 Thanks guys. Somethings wrong though cause now it wont even work when I take out the third condition. I can't figure out what it could be. I put the id's onto each form element. Link to comment Share on other sites More sharing options...
sunflower Posted September 18, 2010 Author Share Posted September 18, 2010 Wait I got it working now it works with the first two conditions it still isnt working with the charAt one though. Link to comment Share on other sites More sharing options...
sunflower Posted September 18, 2010 Author Share Posted September 18, 2010 Wait now it is working! :DDD Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.