Forum Moderators: open

Message Too Old, No Replies

Field Arguments to a Function in Javascript

The argument is supposed to be a FIELD no a value

         

pcwow

9:59 pm on May 11, 2009 (gmt 0)

10+ Year Member



in my script.js file there is a function to set the language of the messages....

function language(field)
{
var x = document.forms.getElementByName(field).value;
return x;
}

there is a input hidden field into my form named as follows...
<input type="hidden" name="vcontact" id="vcontact" value="0">

I call the functions this way when validating the form...

if(language("vcontact") == 0)
nameInfo.text("enter a name with more than 3 letters!")
else
nameInfo.text("digite um nome com mais de 3 letras!");

0 = US (english messages)
1 = BR (portuguese messages)

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)
Timestamp: Mon, 11 May 2009 21:57:17 UTC

Message: Object doesn't support this property or method

PLEASE HELP me with this ...

rocknbil

3:19 am on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try renaming function language to something else?

function lang(.....

if(lang("vcontact") == 0).....

I could be wrong, might be a reserved word?

pcwow

3:46 am on May 12, 2009 (gmt 0)

10+ Year Member



function langcheck(field)
{
var x = document.forms.getElementByName(field).value;
return x;
}

changed the name as you can see, but it still giving me the same error....
thanks to join me in this journey!
It's always challenging to find answers...

rocknbil

3:54 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not really sure what you're trying to do. You should use documentGetElementById instead, might have more luck in the Javascript forum. Working code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd">
<!-- put doctype all on one line -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
function checkLen(form) {
if (document.getElementById) {
var x = (document.getElementById('lang-en').checked==true)?0:1;
var d = document.getElementById('nameInfo');
var e = document.getElementById('warn');
var val = document.getElementById('username').value;
if (val.length < 4) {
e.style.display="block";
if (x == 0) { d.value='enter a name with more than 3 letters!'; }
else { d.value='digite um nome com mais de 3 letras!'; }
//alert(d.value);
}
else { form.submit(); }
}
return false;
}
function clearField() {
document.getElementById('nameInfo').value='';
var e = document.getElementById('warn');
e.style.display="none";
}
</script>
</head>
<body>
<form method="post" action="" onSubmit="return checkLen(this);">
<p><input type="radio" name="lang" id="lang-en" value="0" onClick="clearField();" checked>
<label for="lang-en">US</label>
<input type="radio" name="lang" id="lang-br" onClick="clearField();" value="1">
<label for="lang-en">BR</label></p>
<p><input type="text" name="username" id="username" value=""></p>
<p id="warn" style="display:none">
<input type="text" name="nameInfo" id="nameInfo" size="55" value=""
onFocus="blur()" style="border:none;">
</p>
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>

pcwow

7:55 pm on May 12, 2009 (gmt 0)

10+ Year Member



iside my html page (form)

<input type="hidden" name="vcontact" id="vcontact" value="0">

inside my script.js file

function langcheck(field)
{
var x = document.forms.getElementByName(field).value;
return x;
}

I will use the same function for different forms, so I pass to the function the NAME of the control (vcontact) like

var mycomp = langckeck('vcontact');

Therefore inside my function, (field) is supposed to carry "vcontact" control, and x must return 0, what will end with mycomp = 0... But it is failing!

If I have to do the reference inside my function again to the document.forms.getElementByName with the actual name of the control there is no point of using the function with the field agrument...

Fotiman

11:17 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



getElementByName is not a valid DOM function. Instead, use getElementById (since your input element has the same ID attribute as the name attribute).

Alternatively, you could use getElementsByName, which returns a collection of elements with that name. But you'd then need to access the particular element within that collection that you are trying to access.

pcwow

2:41 am on May 13, 2009 (gmt 0)

10+ Year Member



Thank you Fotiman, but could you show me how? I exausted all my alternatives...

Fotiman

4:15 am on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




function langcheck(field)
{
var x = document.getElementById(field);
return (x != null? x.value: null);
}

Hope that helps.

pcwow

5:04 am on May 13, 2009 (gmt 0)

10+ Year Member



all this pain, and it was just a little detail:

if(langcheck('vcontac') == 0) works

if(langcheck("vcontac") == 0) DOES NOT!

function langcheck(field) { var x = document.getElementById(field); return (x != null? x.value: null); }
returns undefined value...

the original function was just fine, except by the little detail... ;) Never again!

Thanks very much for all of your time ad effort to make my life easier...