...

View Full Version : Passing parameter in function



cww2
05-07-2008, 01:49 AM
Hi - I've tried passing this hundreds of different ways without effect. In a nutshell, I have an HTML page that is generated dynamically from db and has form elements with the same name plus the numeric ID from the db tacked on the end to make the form elements individual.

With JS, I've been able to determine which form elements need to be processed. Where I'm falling short is when I try to validate those form elements, my code breaks down:


function addrsvp(str)
{
var event_id = +str;
if (Record_Validator(''+event_id+'') == true)
{
document.frm.action = "?action=addrsvp&id="+event_id
document.frm.method = "post"
document.frm.submit()
}
}

// This part works fine - with the Record_Validator, just using the alert box, I've verified "ev_id" is being passed correctly. Where it breaks down is the if statement adding the ev_id to the end of "first" - tried everything I could find:

function Record_Validator(strg)
{
var ev_id = +strg
if (document.frm.first+ev_id+.value == "")
{
alert(''+ev_id+'');
return (false);
}

return true;
}
Any suggestions will be tremendously appreciated.

Chase

binaryWeapon
05-07-2008, 02:11 AM
Would this work at all?



function Record_Validator(strg)
{
var ev_id = +strg;
fieldname='first'+ev_id;
if (document.frm.fieldname.value == "")
{
alert(''+ev_id+'');
return (false);
}

Also: just so you know, on your alert: alert(''+ev_id+'');, you can just put alert(ev_id);, and when you call ev_id you can just put var ev_id = strg;

cww2
05-07-2008, 02:20 AM
Would this work at all?

[code]
function Record_Validator(strg)
{
var ev_id = +strg;
fieldname='first'+ev_id;
if (document.frm.fieldname.value == "")



Thanks for the overall tips. Much appreciated.

The above modification gives me the error "document.frm.fieldname.value is null or not an object"

Really appreciate the help.

Chase

cww2
05-07-2008, 03:14 AM
BTW - with document.write - I was able to verify that "fieldname = "first"+ev_id " returned the desired fieldname. Still can't figure out integrate that to work correctly -- document.frm.fieldname.value == "" ....

Chase

binaryWeapon
05-07-2008, 03:18 AM
OK this might not work at all, I would just suggest trying putting your javascript below your form if thats possible. I don't know why but sometimes when I had my form after my javascript it wouldn't recognize the form or the fields.

This might not be the problem, but I wanted to mention it.

GL

cww2
05-07-2008, 03:24 AM
Thanks again GL. If I hardcode it to what I know will be one of the form fields - document.frm.first6.value == "" - then all is cool and works as it should.

I guess I maybe should have thought of asking this earlier but should it make a difference that the parameter is always numeric?

Chase

cww2
05-07-2008, 03:54 AM
Finally found something that worked:

if (document.getElementById("first" + strg).value == "")

Thanks again for all the help. You're very kind to take the time.

Chase



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum