473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

function in a variable

hi all,
I'm writing a function that, say, loop through input fields in a form
and set default values to it. Some fields are dependent thus I have
some onChange scripts to kick off corresponding actions to those
fields.

<form name="test">
<input type="text" name="text1">
<input type="text" name="text2" onChange="text1.value+=this.value;">
<input type="text" name="text3" onChange="doSomething();">
</form>

<script language="JavaScript">
function setDefault(form) {
var fields = form.elements;
for (var i=0; i<fields.length; i++) {
// for each field, somehow get a default value and set it.
}
}
</script>
......

[OnChange] catches only browser actions as far as I understand. That
means when I set a value to "text2" via the method, the corresponding
action isn't triggered.

I can get the actions of a corresponding field by:
var actn = field.onchange;
alert( actn );

which shows:
anonymous() {
doSomething();
}
Despite being able to get all those actions and store them in a
variable, I don't know how to execute them. Do anyone has a clue?
Thanks very much
George
Jul 23 '05 #1
8 1489
Hello

"xxgeorge" <xx******@yahoo.com> wrote in message
news:e9**************************@posting.google.c om...
hi all,
I'm writing a function that, say, loop through input fields in a form
and set default values to it. Some fields are dependent thus I have
some onChange scripts to kick off corresponding actions to those
fields.

<form name="test">
<input type="text" name="text1">
<input type="text" name="text2" onChange="text1.value+=this.value;">
<input type="text" name="text3" onChange="doSomething();">
</form>

<script language="JavaScript">
function setDefault(form) {
var fields = form.elements;
for (var i=0; i<fields.length; i++) {
// for each field, somehow get a default value and set it.
}
}
</script>
.....

[OnChange] catches only browser actions as far as I understand. That
means when I set a value to "text2" via the method, the corresponding
action isn't triggered.

I can get the actions of a corresponding field by:
var actn = field.onchange;
alert( actn );

which shows:
anonymous() {
doSomething();
}
Despite being able to get all those actions and store them in a
variable, I don't know how to execute them. Do anyone has a clue?

Hmm...

You wanted to execute a given action?

Why not call it directly w/o storing it in a variable as:
field.onChange(field); // <-- sending it the 'this' or itself
or
field.onChange();

HTH,
Elias
Jul 23 '05 #2
On Wed, 11 Aug 2004 11:05:04 +0300, lallous <la*****@lgwm.org> wrote:

[snip]
Why not call it directly w/o storing it in a variable as:
field.onChange(field); // <-- sending it the 'this' or itself
or
field.onChange();


Remember that the properties are case-sensitive, unlike HTML. It should be:

field.onchange();

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #3
"lallous" <la*****@lgwm.org> wrote in message news:<2n************@uni-berlin.de>...
Hello

Hmm...

You wanted to execute a given action?

Why not call it directly w/o storing it in a variable as:
field.onChange(field); // <-- sending it the 'this' or itself
or
field.onChange();

HTH,
Elias

oh thx, that do the trick. how I can miss this :)
Anyway, just for interest, still want to know if there's any way to
invoke a method obj. I mean something like "mthd.call()".

One more question. is there any way to list out all fields and
methods of an object? say I have the "mthd" object, how can I know
its attributes?
many thx
xxgeorge
Jul 23 '05 #4
"xxgeorge" <xx******@yahoo.com> wrote in message
news:e9*************************@posting.google.co m...
"lallous" <la*****@lgwm.org> wrote in message

news:<2n************@uni-berlin.de>...
Hello

Hmm...

You wanted to execute a given action?

Why not call it directly w/o storing it in a variable as:
field.onChange(field); // <-- sending it the 'this' or itself
or
field.onChange();

HTH,
Elias

oh thx, that do the trick. how I can miss this :)
Anyway, just for interest, still want to know if there's any way to
invoke a method obj. I mean something like "mthd.call()".

One more question. is there any way to list out all fields and
methods of an object? say I have the "mthd" object, how can I know
its attributes?


<script language="JavaScript">
<!--
for (x in document)
document.write(x + "<br>");
//-->
</script>

--
Elias
Jul 23 '05 #5
>
One more question. is there any way to list out all fields and
methods of an object? say I have the "mthd" object, how can I know
its attributes?


Firefox has an object viewer.

http://www.mozilla.org/products/firefox/

You can loop through all the first level members of an object with:

for (i in obj)
{
}
You would have to put in check for nested objects etc.

Robert
Jul 23 '05 #6
xx******@yahoo.com (xxgeorge) wrote:
"lallous" <la*****@lgwm.org> wrote in message news:<2n************@uni-berlin.de>...
Hello

Anyway, just for interest, still want to know if there's any way to
invoke a method obj. I mean something like "mthd.call()".


function f() { alert('hello') }
mthd = f;
mthd();

Regards,
Steve
Jul 23 '05 #7
hi all,
thx for all your response. I now have another question. I'm trying
to write a similar piece of script that automatically turns all text
field data into uppercase. Here's the script I put at the bottom of
my page:

<script language="JavaScript">
var form = window.document.myForm;
var fields = form.elements;
for (var i=0; i<fields.length; i++) {
var field = fields[i];
if (field.type.toUppercase().indexOf("TEXT")>=0) {
field.onblur = toUpperCase;
}
}
function toUpperCase() {
var field = window.event.srcElement;
field.value = field.value.toUpperCase();
}
</script>

It works fine but the question is, what if the field originally has a
onBlur function? how can I tell it to do both it's own function and
mine?
Thanks for your help
george
Jul 23 '05 #8
On 19 Aug 2004 21:30:57 -0700, xxgeorge <xx******@yahoo.com> wrote:
thx for all your response. I now have another question. I'm trying
to write a similar piece of script that automatically turns all text
field data into uppercase. Here's the script I put at the bottom of
my page:

<script language="JavaScript">
Valid HTML requires the use of the type attribute. The language attribute
has been deprecated and should not be used.

<script type="text/javascript">
var form = window.document.myForm;
No need to use window.
var fields = form.elements;
for (var i=0; i<fields.length; i++) {
var field = fields[i];
if (field.type.toUppercase().indexOf("TEXT")>=0) {
The type property returns a lowercase string. There is no reason to
convert it to uppercase. Also, if the field is an INPUT of type text, only
'text' will be returned.

if(field.type == 'text') {
field.onblur = toUpperCase;
}
}
function toUpperCase() {
var field = window.event.srcElement;
field.value = field.value.toUpperCase();
You should be able to change that to

this.value = this.value.toUpperCase();

as the this operator will reference the object that is handling the event
(the text field).
}
</script>

It works fine but the question is, what if the field originally has a
onBlur function? how can I tell it to do both it's own function and
mine?


It depends on your target environment. If this is to be used on an
Intranet, the browsers available will offer different approaches. For
example, IE allows

obj.attachEvent(type, listener) [1]

whilst DOM-compliant browsers offer

obj.addEventListener(type, listener, capture) [2]

However, if this is for the Web, you'll need a much more intricate method.
So, restricted environment, or the Web?

Mike
[1] The type parameter is the event name with the 'on' prefix.
[2] The type parameter is the event name *without* the 'on' prefix.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
5
by: Andrew Poulos | last post by:
I tested the JSON parse/to code from json.org and it works but I don't understand how. Could someone explain how something in this format works: (function() {...})(); Andrew Poulos
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.