sorry for the stupid question, bu6 I haven't been able to find the
answer anywhere..
consider this useless function which assigns an object to a var..
function(myParam){
var select1 = document.myForm.mySelectName;
}
I simply want this line to subsitute "mySelectName" with a var string
function(myParam){
var select1 = document.myForm.myParam;
}
It doesn't work, ive tried umpteen variations with []+ "" '' etc..
How would I do this stupid-simple thing? 22 1454
bmgz wrote: function(myParam){ var select1 = document.myForm.myParam;
var select1 = document.myForm.elements[myParam];
but in most cases you can simply pass around objects like for instance
the form or select itself, there is hardly the need to pass in a string
to use it to find an object reference.
--
Martin Honnen http://JavaScript.FAQTs.com/
Each object inherits hash (associative array) mechanics, this is how
objects properties are stored. So:
function f1(propertyName) {
var myVar = someObject[propertyName];
// in the particular:
// var myVar = document.formName[propertyName];
}
bmgz wrote:
[...] function(myParam){ var select1 = document.myForm.myParam; }
It doesn't work, ive tried umpteen variations with []+ "" '' etc.. How would I do this stupid-simple thing?
Are you passing an object or a string?
Mick
Mick White wrote: bmgz wrote: [...]
function(myParam){ var select1 = document.myForm.myParam; }
It doesn't work, ive tried umpteen variations with []+ "" '' etc.. How would I do this stupid-simple thing?
Are you passing an object or a string?
Mick
An object, I want to know how to refer to the object that triggered the
function?
bmgz wrote: Mick White wrote:
bmgz wrote: [...]
function(myParam){ var select1 = document.myForm.myParam; }
It doesn't work, ive tried umpteen variations with []+ "" '' etc.. How would I do this stupid-simple thing?
Are you passing an object or a string?
Mick
An object, I want to know how to refer to the object that triggered the function?
oh um, from within the function itself..
ie.
function(){
var a = "[the object that called me ie. myButton]";
}
<input type="button" name="mybutton" onclick='function();' />
bmgz wrote:
[...] An object, I want to know how to refer to the object that triggered the function?
oh um, from within the function itself.. ie.
function(){ var a = "[the object that called me ie. myButton]"; }
<input type="button" name="mybutton" onclick='function();' />
You have to pass a reference in your onclick:
<input type="button" name="mybutton" onclick="function(this);" />
in the script of your onclick use 'this' ---------------^^^^
Then in your function:
function( a ){
alert( 'the object that called me is ' + a );
...
}
or
function(){
alert( 'the object that called me is ' + arguments[0] );
...
}
--
Rob
> want to know how to refer to the object that triggered the function? from within the function itself..
Oh I see now... It depends on how your function was called: by event
capturer or by another function during normal program flow.
If by event capturer, then you simply *cannot* do it.
At least nothing reliable that would work for all browsers. You can do:
function myFunction(e) {
// "e" argument is important placeholder
// in case of working in NN/DOM event model
// IE sets global "event" variable for you
var myCaller = (e)? e.currentTarget : event.srcElement;
}
Of course you have to add event capturers programmically in this case.
You cannot do <element onclick="myFunction()">,
Empty parenthesis will kill the "e" and the script will fail for FF &
Co
You have to:
obj.onclick = myFunction;
or
using addEventListener (DOM) / attachEvent (IE) methods
This works fine as long as there is no more elements below the element
that capturing events. Otherwise all this mess fails onto the ground.
In FF you still can get the right object by using currentTarget. But in
IE event.srcElement will point to the first element in the bubble
history. So in the majority of situations your only option is to use
intrinsic capturers and "manually" forward the right object to the
function: <element onclick="myFunction(this)">
If your function is called by another function during normal program
flow, you can check the caller property.
function myFunction {
if (myFunction.caller != null) {
myCaller = myFunction.caller
}
else {
// I'm the first one!
}
}
P.S. There is a lot of bubbles in docs about "caller is deprecated".
But deprecation doesn't mean "removal", it means "substitution". As
long as *no one* provided a reasonable substitution for caller, we just
keep using it. It's supported by all main browsers.
On 19/06/2005 12:58, VK wrote: want to know how to refer to the object that triggered the function? from within the function itself..
[snip]
If by event capturer, then you simply *cannot* do it.
Of course you can.
<... onclick="myFunction(this);">
function myFunction(element) {
/* The element argument is a reference to the HTML element */
}
Or:
<... id="myElement">
document.getElementById('myElement').addEventListe ner(
'click',
function() {
/* The this operator refers to the HTML element, myElement */
},
false
);
Or:
<... id="myElement">
document.getElementById('myElement').onclick = function() {
/* The this operator refers to the HTML element, myElement */
};
function myFunction(e) {
[...]
var myCaller = (e)? e.currentTarget : event.srcElement; }
You've been told before that referring directly to the event identifier
is not wise as it could be undefined in some browsers. Refer to it via
the global object and test that it exists.
Also, the above could produce very wrong results: the currentTarget and
srcElement properties refer to very different things. The former is the
same as the element variable or the this operator in my examples, whilst
the latter (and the target property) refer to the original element that
fired the event.
Of course you have to add event capturers programmically in this case. You cannot do <element onclick="myFunction()">, Empty parenthesis will kill the "e"
So instead, one could write:
<... onclick="myFunction(event);">
or to be really cautious:
<... onclick="myFunction(('object' == typeof event) && event);">
The first argument to myFunction will either be false or an event object.
[snip]
P.S. There is a lot of bubbles in docs about "caller is deprecated". But deprecation doesn't mean "removal", it means "substitution".
Deprecation often means this feature may be removed in the future, and
more generally, this feature should not be used.
As far as ECMAScript is concerned, the caller property has never
existed, which is potentially a bigger concern than whether or not it is
deprecated.
Finally, the caller property doesn't provide the information that the OP
wants, at least as I understand the problem.
As long as *no one* provided a reasonable substitution for caller, we just keep using it.
If a function must really need to know what called it, then it should
have a required argument that contains a reference to the relevant
function object. No-one needs to provide a substitute for the caller
property as it isn't necessary.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
>> from within the function itself.
That was the keyword in OP. The "manual feeding" of the right object
was already given by RobG.
But what if we need to keep our HTML/XML code and scripting totally
separate?
The code below illustrates the problem many posters in this group
really encounting (even w/o knowing it):
<html>
<head>
<title>Event target</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
function init() {
document.getElementById('DIV1').onclick = f1;
}
function f1(e) {
var myTarget = ((e)&&(e.currentTarget))?
e.currentTarget : event.srcElement
alert(myTarget.id);
}
window.onload = init;
</script>
</head>
<body>
<div id="DIV1">DIV 1
<span id="SPAN1">SPAN 1</span>
</div>
</body>
</html>
Click on "SPAN 1" (nested element). FF still works properly grace to
the e.currentTarget property. This is actually why you should not use
e.target unless you're making some event tracking.
e.target will fail in any more or less complicated situation.
event.srcElement shows as before the original event source (SPAN1).
There is *no* way to tell from the function itself that the actual
event capturer is DIV1. The only side walk is using closures with
memory leak in IE.
You find a way (w/o closures, w/o inline "helpers") - you'll be better
than the whole Microsoft. ;-)
On 19/06/2005 17:12, VK wrote:
[snip] But what if we need to keep our HTML/XML code and scripting totally separate?
I just demonstrated how to accomodate that, and I've done it several
times in the past, too.
[snip]
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
> I just demonstrated how to accomodate that, and I've done it several times in the past, too.
(1) <... onclick="myFunction(this);">
Manual feeding of the right object to the function I just mentioned.
Not an option in many cases (like OP)
(2) document.getElementById('myEle*ment').addEventList ener(
'click',
function() {
/* The this operator refers to the HTML element, myElement */
},
false
);
A closure I just mentioned. A memory leak in IE. Can spit on it I guess
in *small-scale* projects(?)
(3) document.getElementById('myEle*ment').onclick = function() {
/* The this operator refers to the HTML element, myElement */
};
Anonymous functions... May get really ugly in big projects. Also forget
about reusable .js libraries you could use in your code. Of course you
could wrap named functions, but it brings you right back to the
situation (2)
On a *small scale* is well usable though.
So (2) and (3) may be used by OP unless "this and that". Should be
close the topic?
On 19/06/2005 20:02, VK wrote:
[snip] (1) <... onclick="myFunction(this);">
I wasn't referring to that one.
[snip]
(2) document.getElementById('myEle*ment').addEventList ener( 'click', function() { /* The this operator refers to the HTML element, myElement */ }, false );
A closure I just mentioned.
That isn't a closure. That is two function calls with a string argument
passed in the first call, and a string, function expression, and boolean
passed in the second.
If this code is executed in a function then the function expression will
become an inner function and, as it survives after the outer call
returns, it will form a closure. However, that isn't an issue in itself
and, as written...
A memory leak in IE.
....the problem in IE that you refer to will not occur.
Given a more realistic implementation:
function listener(e) {
/* Some event listener */
}
function addListener(elementId) {
var element;
if(document.getElementById) {
element = document.getElementById(elementId);
}
if(element && element.addEventListener) {
element.addEventListener('click', listener, false);
}
}
there is still no problem as a closure isn't necessary. Altering it
slightly to use a closure (because sometimes they are necessary):
function addListener(elementId) {
var element;
if(document.getElementById) {
element = document.getElementById(elementId);
}
if(element && element.addEventListener) {
element.addEventListener('click', function(e) {
/* Some event listener */
}, false);
}
}
we now do have the potential for a memory leak in IE because the
reference in element will persist. However, that is trivial to deal with:
function addListener(elementId) {
/* ... */
element = null;
}
No circular reference. No memory leak.
Even if element had to remain for the benefit of something else (for
example, it's a private member in an object) the reference can be
cleared when the unload event is fired, so we still don't have a problem.
(3) document.getElementById('myEle*ment').onclick = function() { /* The this operator refers to the HTML element, myElement */ };
Anonymous functions... May get really ugly in big projects.
The use of a function expression was for my convenience, just as it was
with the addEventListener example in my previous post. Nothing prevents
the use of function declarations, or function expressions assigned to
identifiers.
Also forget about reusable .js libraries you could use in your code.
I have no idea what makes you think that, unless you're using some badly
designed libraries.
Of course you could wrap named functions, [...]
What!?
[snip]
So (2) and (3) may be used by OP unless "this and that". Should be close the topic?
Perhaps you should just refrain from commenting on it until you know
what you're talking about. I'm sorry for being so harsh, but from
experience in previous threads, you seem incapable of understanding the
issues. Maybe I'm not providing decent explanations, but if that's the
case, you've never said anything to help me help you.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
> Maybe I'm not providing decent explanations, but if that's the case, you've never said anything to help me help you.
Maybe Michael, and sorry if I'm "trolling" sometimes from your point of
view. But this group is not my place of work, but a place to put some
of my thoughts onto the public view and discussion. As long as I follow
the group netiquette, it's fine I guess.
To really close this topic (that really started a few month ago), let
us this question to the ground-zero level:
Below is the HTML. The task: acting only from within scriptboded code
(<script></script> section) insure that no matter where did I click (on
div or span) I always get in f1() the real event capturer (which is
DIV1). You can change the <script></script> section in any way you
want. The <body></body> section is out of your control. No more
quotations. Just the code, the task and you.
<html>
<head>
<title>Event target</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
function init() {
document.getElementById('DIV1'*).onclick = f1;
}
function f1(e) {
var myTarget = ((e)&&(e.currentTarget))?
e.currentTarget : event.srcElement
alert(myTarget.id);
}
window.onload = init;
</script>
</head>
<body>
<div id="DIV1">DIV 1
<span id="SPAN1">SPAN 1</span>
</div>
</body>
</html>
On 20/06/2005 14:21, VK wrote:
[snip] The task: acting only from within scriptboded code (<script></script> section) insure that no matter where did I click (on div or span) I always get in f1() the real event capturer (which is DIV1).
[snip]
At its simplest:
<script type="text/javascript">
function f1() {
alert(this.id);
}
window.onload = function() {
document.getElementById('DIV1').onclick = f1;
};
</script>
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
<html>
<head>
<title>Event target</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
function f1() {
alert(this.id);
}
window.onload = function() {document.getElementById('DIV1').onclick =
f1;};
</script>
</head>
<body>
<div id="DIV1">DIV 1
<span id="SPAN1">SPAN 1</span>
</div>
</body>
</html>
Michael Winter,
if it's not a circular reference as you say,
then your're my hero, and I'm an a**hole
VK wrote:
<snip> ... . As long as I follow the group netiquette, it's fine I guess.
<snip>
But you don't follow the group netiquette. the vast majority of your
posts to date have been mal-formed, lacking quoted material to provide
context for the responses. And in creating mal-formed posts you have
encouraged your corespondents to do likewise, diminishing their chances
of getting responses from informed contributors to the group.
Richard.
> But you don't follow the group netiquette. the vast majority of your posts to date have been mal-formed, lacking quoted material to provide context for the responses. And in creating mal-formed posts you have encouraged your corespondents to do likewise, diminishing their chances of getting responses from informed contributors to the group.
I see this fault only in long thread, there are sometimes 20-40 posts.
Should I include the entire thread in each of my post? Or sould I do
some kind of a self-censorship by including only selected posts and
parts? I can start include the entire body of the parent post I'm doing
a follow up for. Would it be a solution? (very inconvenient though as
I'm using Google groups so I have to ">" manually)
On 21/06/2005 14:51, VK wrote:
[Not quoting previous posts] I see this fault only in long thread, there are sometimes 20-40 posts.
It doesn't matter how long a thread is. A reader might want to jump to
the end of a long thread. That reader really shouldn't need to have read
every single post along the way - they should be able to understand the
gist of the conversation at whatever point they enter.
Should I include the entire thread in each of my post? [...]
No. You should do what all of the regulars do when posting: quote any
text that is relevant to the point you are trying to make so it is clear
what you are responding to. Everything else should be deleted,
preferably including some indication that you have omitted something.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
VK wrote: But you don't follow the group netiquette. the vast majority of your posts to date have been mal-formed, lacking quoted material to provide context for the responses. And in creating mal-formed posts you have encouraged your corespondents to do likewise, diminishing their chances of getting responses from informed contributors to the group.
I see this fault only in long thread, there are sometimes 20-40 posts. Should I include the entire thread in each of my post? Or sould I do some kind of a self-censorship by including only selected posts and parts? I can start include the entire body of the parent post I'm doing a follow up for. Would it be a solution? (very inconvenient though as I'm using Google groups so I have to ">" manually)
Please don't quote an entire post. Its only requested that you quote the
parts that you are replying to and leave enough attribution to show who
posted what.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Randy Webb <Hi************@aol.com> writes:
[entire quoted post] Please don't quote an entire post.
That was irony, right? :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Lasse Reichstein Nielsen wrote: Randy Webb <Hi************@aol.com> writes:
[entire quoted post]
Please don't quote an entire post.
That was irony, right? :)
I was wondering who would catch that :)
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
JRS: In article <11**********************@g44g2000cwa.googlegroups .com>
, dated Tue, 21 Jun 2005 06:51:32, seen in news:comp.lang.javascript, VK
<sc**********@yahoo.com> posted : But you don't follow the group netiquette. the vast majority of your posts to date have been mal-formed, lacking quoted material to provide context for the responses. And in creating mal-formed posts you have encouraged your corespondents to do likewise, diminishing their chances of getting responses from informed contributors to the group. I see this fault only in long thread, there are sometimes 20-40 posts. Should I include the entire thread in each of my post?
No. You should read the newsgroup FAQ and follow the advice in it.
Or sould I do some kind of a self-censorship by including only selected posts and parts?
Deciding what needs to be re-posted is part of an author's duty. Read
the FAQ, Wednesday edition.
I can start include the entire body of the parent post I'm doing a follow up for. Would it be a solution?
That would be very silly. Read the FAQ.
(very inconvenient though as I'm using Google groups so I have to ">" manually)
No, AIUI you do not. However, we outnumber you, so our convenience is
more important than yours.
<FAQENTRY>
If you find that, when you start a News reply, Google does not provide
the previous article in quoted form, note what Keith Thompson wrote in
comp.lang.c, message ID <ln************@nuthaus.mib.org> :-
If you want to post a followup via groups.google.com, don't use
the "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sneill |
last post by:
How is it possible to take the value of a variable (in this case,
MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name?
In the...
|
by: XenofeX |
last post by:
How can i call to object from string variable ?
For instance
string x = "textBox1";
i want to call the object which name is stored in the x...
|
by: Miguel Dias Moura |
last post by:
Hello,
I am working on an ASP.NET / VB page and I created a variable "query":
Sub Page_Load(sender As Object, e As System.EventArgs)
Dim query...
|
by: Andres |
last post by:
Hi all,
I have the problem to assign a variable of type object to
a specific class at runtime. I want to use a string
variable that specify the...
|
by: JohnR |
last post by:
I'm trying to find a way to create a variable of a given type at runtime
where I won't know the type until it actually executes. For example,
...
|
by: Russ Chinoy |
last post by:
Hi,
This may be a totally newbie question, but I'm stumped.
If I have a function such as:
function DoSomething(strVarName) {
.....
}
|
by: Neo Geshel |
last post by:
I am trying to deal with an image in code-behind. I consistently get the
following error:
Server Error in '/' Application.
Object variable or...
|
by: Jody Gelowitz |
last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code...
|
by: Don Miller |
last post by:
I am using a Session variable to hold a class object between ASP.NET pages
(in VB). In my constructor I check to see if the Session variable exists,...
|
by: RSH |
last post by:
Hi,
I have a situation where I have created an object that contains
fields,properties and functions. After creating the object I attempted to...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |