using javascript to get a FORM's Name
Moderator: General Moderators
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
using javascript to get a FORM's Name
Hello,
Can you get a FORM's Name?
i am researching a form.name attribute in javascript but i dont no how it is used
Can you get a FORM's Name?
i am researching a form.name attribute in javascript but i dont no how it is used
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
It's a string! Since you have a pointer to the form you can access its name. Try:
Cheers,
Scorphus.
Code: Select all
<html>
<head>
<title>.o0o.</title>
<script language="JavaScript1.2" type="text/javascript">
<!--
function foo (formPtr) {
alert('Form name is: ' + formPtr.name);
formPtr.submit();
return true;
}
//-->
</script>
</head>
<body>
<form action="<?php echo $_SERVERї'PHP_SELF'] ?>" method="POST" name="form1" onSubmit="javascript:foo(this)">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="text" name="email1" value="me@myhost.com" disabled="true"><br>
<input type="hidden" name="email2" value="me@myhost.com">
<input type="submit" name="login" value="Log In">
</form>
<pre>
<?php
print_r($_POST);
?>
</pre>
</body>
</html>Scorphus.
-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
try
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST" name="form1">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="text" name="email1" value="me@myhost.com" disabled="true"><br>
<input type="hidden" name="email2" value="me@myhost.com">
<input type="submit" name="login" value="Log In">
</form>
Code: Select all
<script language="javascript">
var formname = document.forms[0].name;
//now 'formname' has the name of the first form in the HTML doc
</script><input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="text" name="email1" value="me@myhost.com" disabled="true"><br>
<input type="hidden" name="email2" value="me@myhost.com">
<input type="submit" name="login" value="Log In">
</form>
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Im trying to get an textarea's character lenght and im trying to dynamically get the forms name....
the foollowing is the code that executes on a KeyDown or Blur
However i get and error saying the the method or property is not suppported
what would be the correct syntax for using
document.eval(form_name).description.value = desc;
the foollowing is the code that executes on a KeyDown or Blur
Code: Select all
if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOfї"MSIE 5"] != -1){
form_name = document.formsї0].name;
characters = document.eval(form_name).value.length;
eval(form_name).counter.value = characters;
if(characters > 125){
alert('You exceeded the maximum limit of characters for your description');
desc = document.eval(form_name).description.value.substr(0,120)+'...';
document.eval(form_name).description.value = desc;
}
}what would be the correct syntax for using
document.eval(form_name).description.value = desc;
- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
What about:Im trying to get an textarea's character lenght
Code: Select all
<!-- in the head section -->
<script type="text/javascript">
function get_char_num (your_text)
{
if (your_text.length > 1000)
{
alert ("You have more that 1000 characters in your text!");
}
}
</script>
<!-- in the body section -->
<textarea onblur="get_char_num(this.value)"></textarea>edit: by now I have tested it
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Fredix,
Well Fredix i also have a dynamic counter which keeps track of the text lenght as well
I also had the script dynamically reset the text length to the limit any time the length is over the limit.
i jus have a problem in syntax trying to reference the form's name dyanmically
how do i syntax it to work???
Kendall
Well Fredix i also have a dynamic counter which keeps track of the text lenght as well
I also had the script dynamically reset the text length to the limit any time the length is over the limit.
i jus have a problem in syntax trying to reference the form's name dyanmically
Code: Select all
form_name = document.formsї0].name;
document.eval(form_name).description.value = desc;
document.form_name.description.value = desc;Kendall
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
You could try this:
Code: Select all
document.formsї0].description.value = desc;- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
It works depending on the <form> and on its contents.
Here is another example:
Cheers,
Scorphus.
Here is another example:
Code: Select all
<html>
<head>
<title>.o0o.</title>
<script language="JavaScript1.2" type="text/javascript">
<!--
function foo (formPtr) {
alert('Form name is: ' + formPtr.name);
formPtr.submit();
return true;
}
//-->
</script>
</head>
<body>
<form action="<?php echo $_SERVERї'PHP_SELF'] ?>" method="POST" name="form1" onSubmit="javascript:foo(this)">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="text" name="desc" value="Description"><br>
<input type="submit" name="login" value="Log In">
</form>
<pre>
<?php
print_r($_POST);
?>
</pre>
<script language="JavaScript1.2" type="text/javascript">
<!--
var desc = "some data";
document.formsї0].desc.value = desc;
alert('document.formsї0].desc.value: ' + document.formsї0].desc.value);
//-->
</script>
</body>
</html>Scorphus.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Scrophus,
what do you mean by
if the character length is more than the maximum size it crops it to the amount it suppose to be.
does this kind of form applies?
Kendall
what do you mean by
the script is suppose to check the length of characters in the text area so that it doesn't go over the limit i have a input field that dynamically shows the nu,mber of charates inputted. All this happens on an onBlur ot KeyDownIt works depending on the <form> and on its contents.
if the character length is more than the maximum size it crops it to the amount it suppose to be.
does this kind of form applies?
Kendall
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
If you want to update an <input>'s value attribute, then you do document.form[n].%<input>'s name%.value = 'new value'; where 'n' is the number of the form in the order of appearance on the page. So if your form has an <input> named description (<input name="description">) that code works for the purpose of changing the <input>'s value.kendall wrote:Scrophus,
what do you mean byIt works depending on the <form> and on its contents.
Yes it does. Take an example:kendall wrote:the script is suppose to check the length of characters in the text area so that it doesn't go over the limit i have a input field that dynamically shows the nu,mber of charates inputted. All this happens on an onBlur ot KeyDown
if the character length is more than the maximum size it crops it to the amount it suppose to be.
does this kind of form applies?
Kendall
Code: Select all
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Form Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form action="some.php" method="post" name="form1">
Description: <textarea name="description" cols="50" rows="5">testing...</textarea><br />
Length: <input type="text" name="length" readonly="yes" value="0" />
</form>
<script language="JavaScript1.2" type="text/javascript">
<!--
var MAX_LEN = 5;
document.formsї0].length.value = document.formsї0].description.value.length;
alert('Description: ' + document.formsї0].description.value + '\n' +
'Length: ' + document.formsї0].length.value);
if (document.formsї0].description.value.length > 5)
document.formsї0].description.value = document.formsї0].description.value.substr(0, MAX_LEN);
document.formsї0].length.value = document.formsї0].description.value.length;
//-->
</script>
</body>
</html>Add this to your existing code that is called by onKeyDown and onBlur.
Also take a look to the JavaScript Central. There you'll find useful information regarding JavaScript.
Hope that helps. Let me know if it doesn't
Cheers,
Scorphus.
Not sure if this will be any help to you because I lost track of where the topic was going 
This function will return TRUE if the form name matches the input.
There's loads of different <form> things you can do so if this isn't what you're after just drop me an email at si@urbanchaos.net and I can give you some more help.
Laters.
This function will return TRUE if the form name matches the input.
Code: Select all
<head>
<script language="javascript">
function checkMe(form,name)
{
if( typeof form.name != "undefined" )
{
if( form.name == name ) return true;
return false;
}
}
</script>
</head>
<body>
<form name="testForm">
<input type="text" onfocus="if(checkMe(this.form,"BOB")){alert('correct')}
else{alert('not correct')}"><br>
<input type="text" onfocus="if(checkMe(this.form,"testForm")){alert('correct')}else{alert('not correct')}">
</form>
</body>Laters.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Gen-ik/ Scrophus,
im really sorry guys...the reason it didnt work was because i kept forgetting to put back the reference to the input's id...it work's fine now
just incase anyone wants to use and improve on it
Again thanks for your help and sorry about the mistake i made
Kendall
im really sorry guys...the reason it didnt work was because i kept forgetting to put back the reference to the input's id...it work's fine now
Code: Select all
function countCharacters(){
var desc;
var characters;
if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOfї"MSIE 5"] != -1){
characters = document.formsї0].description.value.length;
document.formsї0].counter.value = characters;
if(characters > 125){
alert('You exceeded the maximum limit of characters for your description');
desc = document.formsї0].description.value.substr(0,120)+'...';
document.formsї0].description.value = desc;
}
}
}Again thanks for your help and sorry about the mistake i made
Kendall