Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'this' keyword in reference to form processing using javascript 2

Status
Not open for further replies.

LTeeple

Programmer
Aug 21, 2002
362
CA
Hi all,
I did an advanced search for this particular subject, but didn't get any hits, so I will post the question:

Can someone please explain to me exactly what the javascript term 'this' means, in the context of form processing?

example: onSubmit="return checkForm(this)"

and can I use 'this' to somehow determine the form name?

Thank you!!

[cheers]
Cheers!
Laura
 
this" typically refers to the element itself. in your particular example it refers to the form.

"this.name" would refer to the form name if used like so
onsubmit="foo(this.name)"

or in your function
function foo(bar) {
bar.name;
}

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Thank you for the quick response. Unfortunately, I might need to ask for clarification.

Scenario: I have 2 different forms. Within each form, I want to call the same useful javascript function.

Form 1:
Code:
<form name="places_form" method="post" action="places.php?tableid=<? echo $tableid; ?>" onSubmit="return check_submit(this);">
<select name="cities" onChange="javascript: getPlaces(this.value);clearFields(this);" size="1">
<option value="Choose a City" selected>Choose a City</option>
<option value="all other options..">All other options...</option>
</select>
</form>
and form 2 - which lives on a different page:
Code:
<form id="add" method="post" action="save.php?tableid=$tableid" onSubmit="return CheckForm(this)">\n";
<select name="city" onChange="javascript: getPlaces(this.value);clearFields(this);">
<option value="Choose a City" selected>Choose a City</option>
<option value="all other options..">All other options...</option>
</select>
</form>

The key here is re-using the handy functions in the select onChange event.

Javascript code (dynamically generated):
Code:
function getPlaces(city) {
index = city;
document.forms['places_form'].elements['places'].options.length=0;
if (index == 0){
  document.forms['places_form'].elements['places'].options[0] = new Option('Places will appear after selecting a city');
}
if (index==1){
document.forms["places_form"].elements["places"].options[0] = new Option("Choose a place:");
document.forms["places_form"].elements["places"].options[1] = new Option("Sheraton Suites Calgary Eau Claire",1);
}
(yadda yadda yadda)
}

is there a way for me to use the 'this' object instead of hard-coding the form name?

Thank you so much!

[cheers]
Cheers!
Laura
 
Pass in the form object:

onChange="getPlaces(this.form,this.value);clearFields(this);"

And add it to your function:
Code:
function getPlaces(myform, city) {
index = city;
myform.elements['places'].options.length=0;
if (index == 0){
  myform.elements['places'].options[0] = new Option('Places will appear after selecting a city');
}
if (index==1){
myform.elements["places"].options[0] = new Option("Choose a place:");
myform.elements["places"].options[1] = new Option("Sheraton Suites Calgary Eau Claire",1);
}
(yadda yadda yadda)
}

Adam

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
The answer is always simple...

Thank you Adam, Thank you Jeff.

TGIF!

[cheers]
Cheers!
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top