...

View Full Version : New to JS, need help with function



Fishwagon
12-14-2009, 08:52 PM
Hello all,

I am new to JS, so please excuse n00b question.

I have multiple forms on a page. Each form has a common hidden field, that holds a selected date. Each field is named the same with a number suffix.

For example,

seldate1, seldate2, seldate3, etc...

Each formname is subsequently the same except a suffix.

While I can check to see if the seldate# field is populated for each individual form using individual functions, I prefer to only have one function. So I made a function that accepts two parameters, one for formname, and one for fieldname:

function checkemptydate(formname, fieldname)
{
if ( document.formname.fieldname.value == '' )
{
alert('You have not selected a date to deliver! Please go back and select a date!')
return false;
}
}
</script>

So on each form's onClick, I have this for example:

onClick="javascript:return checkemptydate('teacherslunchbox_1_ATC_0','selDaycal1')"


Where I am failing is trying to use those parameter names in the line that checks the value (since I guess, I am using parameter names instead of the acual object names, so it cannot find the object in question)

if ( document.formname.fieldname.value == '' )

So my question is, how can I use the passed parameters? Or am I doing this the wrong way?

Thanks in advance to everyone...

ckeyrouz
12-14-2009, 09:03 PM
Try replacing your js function with this:


function checkemptydate(formname, fieldname)
{
if ( eval("document."+formname+"."+fieldname+".value") == '' )
{
alert('You have not selected a date to deliver! Please go back and select a date!')
return false;
}
}

Another solution would be passing the object by itself as parameter:



function checkemptydate(myObj)
{
if ( myObj.value == '' )
{
alert('You have not selected a date to deliver! Please go back and select a date!')
return false;
}
}

and call it like this:


onClick="checkemptydate(this)"

Fishwagon
12-14-2009, 09:14 PM
[[[[[hitting self on head]]]]]

Thanks, makes total sense (now)

Appreciate the help very much

Old Pedant
12-14-2009, 11:06 PM
Not to ask a silly question, but...

Why do you make the field names different in the different forms???

There's no reason to.



<form name="form1">
<input type="hidden" name="selDate" />
...
</form>
...
<form name="form27">
<input type="hidden" name="selDate" />
...
</form>

There's no conflict. You can always address any single hidden field via

document.form13.selDate
etc.

And you could choose the correct form quite easily:


function checkemptydate( formNumber )
{
if ( document.forms["form" + formNumber].selDate.value == '' )
{
alert('You have not selected a date to deliver! Please go back and select a date!')
return false;
}
return true; // DO NOT FORGET THIS!!
}

Or, of course:


function checkemptydate( form )
{
if ( form.selDate.value == '' )
{
alert('You have not selected a date to deliver! Please go back and select a date!')
return false;
}
return true; // DO NOT FORGET THIS!!
}

and then

onClick="javascript:return checkemptydate(this.form);"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum