PDA

View Full Version : syntax problem with document.something...


szewaili
04-28-2005, 02:02 PM
I know I can call for the value of a certain element by doing this:

var vlu = document.form.elementName.value;

What if I have this called in a function with arguments in an external js file? can I put:

function called(x, y)
{
var vlu = document.x.y.value;
return vlu;
}

I can't seem to make it work. I seem to take x and y as the name of the element instead of a variable that contain the element's name. How can I make this work?

sincerely,

Silvia

Kor
04-28-2005, 02:15 PM
document.forms['formnameorindex'].elements['elementnameorindex']

Kor
04-28-2005, 02:19 PM
example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
onload = function(){alert(document.forms[0].elements['txt'].value)}
</script>
</head>
<body>
<form>
<input name="txt" type="text" value="blah">
</form>
</body>
</html>

szewaili
04-28-2005, 03:38 PM
This is Great! Thanks a lot!

Here's a second question for you:

how do I set maximum character limit on a textarea, using javascript?

I tried setting the readonly property after the user reached the limit, but the user can keep typing.

I also tried using the disabled property, but that totally disabled the textarea, user can't go back to modify the textarea.

How can I stop the user from typing when it reaches the character limit?

Kor
04-28-2005, 04:14 PM
here's an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var maxL = 10;//max characters number allowed
function len(t){
var ml = document.forms[0].elements['le'];
var v = new String();
var s = t.value.split('');
for(i=0;i<maxL;i++){v+=s[i]}
if(s.length>=maxL){t.value=v;ml.value = maxL-v.length;}
ml.value = maxL-s.length;
}
}
</script>
</head>
<body>
<form>
<textarea cols="30" rows="4" onkeyup="len(this)"></textarea>
<br>
<input name="le" type="text" size="4" readonly="true">
... characters left
</form>
</body>
</html>

szewaili
04-28-2005, 04:26 PM
Thanks again! you're genius!