Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic (I think)

Status
Not open for further replies.

DarkConsultant

Programmer
Dec 4, 2007
156
GB
Hi,

I have inherited a project which involves some javascript. I am a bit of a newbe and need to ask a question. Sorry if this is a daft question.

I have a function called StringRandom that generates a random password string. Is there any way to pass a parameter that refers to a field with the same form.

So if I have eight fields in my form called passfield1 - 8 can I call StringRandom("passfield1") for example and get the random string placed into passfield1. I have achieved this by passing 1 thru 8 as a parameter and using a long if .. elseif (sounds of retching) block but I dont really like this.

Sorry if this is a bit basic but I have tried for about an hour seaching but do not think I know the correct words to search for. The last time I touched javascript was about 10 years ago.

TIA

David Wadsworth


DarkConsultant

Live long and prosper \\//
 
Pass the same number, and just dynamically construct the ID for the element you wish to target like:

Code:
function StringRandom(PFnum){
var textbox=document.getElementById('passfield' + PFnum);

...

}

This automatically targets the field with an ID of passfield plus a number. StringRandom(2) for instance would produce the string in the field with an ID of passfield[red]2[/red]
ad StringRandom(7) would target passfield[red]7[/red].


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Ummmmmm seemed so perfect I didnt try it first .... and it didnt work.

Should this have worked?

function StringRandom(PFnum){
var textbox=document.getElementById('passfield' + PFnum);
var sometext="yada";

textbox.value=sometext;
}

DarkConsultant

Live long and prosper \\//
 
Yup, that's correct.

How are you calling the function?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
onClick='RandomString("0");'

I can get it to function if I remove the 'var textbox=document.getElementById('passfield' + PFnum);' part.

If I put an alert box in I find I am passing the correct field number and the correct field name is being used. It is as if the var assignation throws an error.

I have searched this and found lots of folk saying the same as you but I cannot get it to work.

DarkConsultant

Live long and prosper \\//
 
OK I got it ....


I changed the name of the variable from textbox to MyTextbox and it works fine. Must be a reserved word.

Sorry mate I am such a nubee at this JS.

Thanks for all of your help.

David

DarkConsultant

Live long and prosper \\//
 
Glad you sorted it out.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top