473,387 Members | 1,464 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,387 software developers and data experts.

attachEvent

Novice here - using notepad only .....

function MenuBarIn(E)
{

var i;

with (document.getElementById(E.id))
{

style.backgroundColor = "steelblue";
style.top = "90%";
style.height = "10%";
style.borderTopStyle = "solid";
style.borderRightStyle = "solid";
style.borderBottomStyle = "solid";
style.borderLeftStyle = "solid";
style.borderTopWidth = "2px";
style.borderRightWidth = "1px";
style.borderBottomWidth = "1px";
style.borderLeftWidth = "1px";
style.borderTopColor = "rgb(102,204,255)";
style.borderRightColor ="rgb(102,204,255)";
style.borderBottomColor ="rgb(36,161,232)";
style.borderLeftColor = "rgb(36,161,232)";

}

// global var
if (giSpanCnt < 14 )
{

// attempt to add 14 span id's each with an Event
for ( i = 0; i < 14 ; i++ )
{
var eleSpan = document.createElement( "SPAN" ) ;
giSpanCnt = giSpanCnt + 1 ;
eleSpan.innerHTML = goMenuArray[giSpanCnt] ;

eleSpan.id = eleSpan.uniqueID ;
eleSpan.className = gClassNameMenuObjects ;
//alert(eleSpan.id) ;
//eleSpan.id = "IdSpan" + giSpanCnt ;

//var curID = document.getElementById(E.id).lastChild ;
document.getElementById(E.id).appendChild(eleSpan) ;

// ERROR HERE - seems to attach for the First eleSpan but not the rest
// IOW - if the mouse enters the first Span I get back INCREASE - but then
I get back TypeMisMatch error
//eleSpan.attachEvent( "onMouseOver" , IncText() ) ;
}
}
}

function IncText()
{
alert("Increase");
}

All help appreciated ......

John McCarthy


Jul 20 '05 #1
4 5410
"John McCarthy" <de****@earthlink.net> writes:
// ERROR HERE - seems to attach for the First eleSpan but not the rest
// IOW - if the mouse enters the first Span I get back INCREASE - but then
I get back TypeMisMatch error
//eleSpan.attachEvent( "onMouseOver" , IncText() ) ;


^ you call the function here
It should be
eleSpan.attachEvent("onmouseover",IncText);
That will bind the function itself to the mouseover event, not the result
of calling it.
You should know that attachEvent *only* works in IE. The W3C DOM contains
functions to do the same for compliant browsers:
eleSpan.addEventListener("mouseover",IncText,false );

Good luck
/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.'
Jul 20 '05 #2
Since Lasse already posted the solution, I will only pay attention to
the things that can be improved.

John McCarthy wrote:
Novice here - using notepad only .....
Notepad is your friend. You should stick to Notepad or use only source
code editors (I would recommend either eclipse with JavaScript Editor
Plugin 0.0.3 or Dreamweaver MX 6.1 using Homesite/Coders layout). And
never trust (JavaScript) source code you do not fully understand. Most
of it, especially on the Web, is badly flawed and gets you more in
trouble. Copy & pray does not pay. (Hey, I like the sound of that one!
:-))
function MenuBarIn(E)
{

var i;

with (document.getElementById(E.id))
Always check objects and properties before you access them:

if (document.getElementById) // Does such a property exist?
{ // if yes
var o = document.getElementById(E.id); // use it
if (o) // if successful
{
// do something
}
// do something more
}

See also http://pointedears.de.vu/scripts/test/whatami

But you do *not* need getElementById(...) here, since you are already
passing the object reference with `E' (`E.id' tells that):

if (E)
{
with (E)
{
// ...
}
}

is sufficient here.
{

style.backgroundColor = "steelblue";
[...]
style.borderLeftColor = "rgb(36,161,232)";
Since you always refer to the `style' property within the block,
you could equally use

if (E && E.style)
{
with (E.style)
{
// ...
}
}

However, if you can avoid it, do not use the `with' statement.
It is likely to produce script errors if not used thoughtfully
and is therefore deprecated in JavaScript 1.5. Reference the
object you intend to use instead:

// if the referenced object has the `style' property
if (E && typeof E.style != "undefined")
{
var s = E.style;
s.backgroundColor = ...
// ...
s.borderLeftColor = ...
}

It is good practice to watch for the case of identifiers. Usually
constructor functions should start with a capital letter while
properties and object references should not. Thus `e' instead of
`E' and `menuBarIn' instead of `MenuBarIn' is recommended here.
}
// global var
if (giSpanCnt < 14 )
Avoid global variables where you can. With this function, you could
have passed the value of `giSpanCnt' as second argument and let the
function return the changed value. You could have also passed a
reference to a container object as second argument and could change
the property of the object instead. In the latter case, you could
have left out the return of the new value.
{

// attempt to add 14 span id's each with an Event
for ( i = 0; i < 14 ; i++ )
{
var eleSpan = document.createElement( "SPAN" ) ;
giSpanCnt = giSpanCnt + 1 ;
eleSpan.innerHTML = goMenuArray[giSpanCnt] ;

eleSpan.id = eleSpan.uniqueID ;
eleSpan.className = gClassNameMenuObjects ;
//alert(eleSpan.id) ;
//eleSpan.id = "IdSpan" + giSpanCnt ;

//var curID = document.getElementById(E.id).lastChild ;
document.getElementById(E.id).appendChild(eleSpan) ;

// ERROR HERE - seems to attach for the First eleSpan but not the rest
// IOW - if the mouse enters the first Span I get back INCREASE - but then
I get back TypeMisMatch error
//eleSpan.attachEvent( "onMouseOver" , IncText() ) ;


Get used to a coding style that is easily readable and editable.
This also helps you from staying away from trouble: Not that much
work to change it, and omitted whitespace reduces file size while
errors with flawed implementations are less likely. Style also
includes consequent indentation of blocks (with space, not with tab),
e.g. the above should read (ignoring what is semantically wrong
with the code for now):

for (i = 0; i < 14; i++)
{
var eleSpan = document.createElement("SPAN");
giSpanCnt = giSpanCnt + 1;
eleSpan.innerHTML = goMenuArray[giSpanCnt];

eleSpan.id = eleSpan.uniqueID;
eleSpan.className = gClassNameMenuObjects;
//alert(eleSpan.id);
//eleSpan.id = "IdSpan" + giSpanCnt;

//var curID = document.getElementById(E.id).lastChild;
document.getElementById(E.id).appendChild(eleSpan) ;

// ...
}
PointedEars
Jul 20 '05 #3
Lasse Reichstein Nielsen wrote:
I get back TypeMisMatch error
//eleSpan.attachEvent( "onMouseOver" , IncText() ) ;

^ you call the function here
It should be
eleSpan.attachEvent("onmouseover",IncText);
That will bind the function itself to the mouseover event, not the result
of calling it.
You should know that attachEvent *only* works in IE. The W3C DOM contains
functions to do the same for compliant browsers:
eleSpan.addEventListener("mouseover",IncText,false );


I could be mistaken, since I haven't done complex DOM work in a long
time, but wouldn't the following work well in both browsers:
eleSpan.onmouseover = IncText();

Jul 20 '05 #4
Keith Bowes wrote:
Lasse Reichstein Nielsen wrote:
I get back TypeMisMatch error
//eleSpan.attachEvent( "onMouseOver" , IncText() ) ;

^ you call the function here
It should be
eleSpan.attachEvent("onmouseover",IncText);
That will bind the function itself to the mouseover event, not the result
of calling it.
You should know that attachEvent *only* works in IE. The W3C DOM contains
functions to do the same for compliant browsers:
eleSpan.addEventListener("mouseover",IncText,false );


I could be mistaken, since I haven't done complex DOM work in a long
time, but wouldn't the following work well in both browsers:
eleSpan.onmouseover = IncText();


Same problem. The event handler is still asigned the *return value*
of the function (since the call operator is used), not the function
reference. Thus

eleSpan.onmouseover = IncText;

should work as supposed.
PointedEars
Jul 20 '05 #5

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

Similar topics

6
by: Adrian Parker | last post by:
Using asp.net 2003 When I use document.attachEvent("onmousemove", resetTimer); The first time into a page, it works fine, but the 2nd time the page is loaded (not postback) it gives a...
2
by: hifchan | last post by:
I'd like to attach an event to a group of possible controls, each event's handling function might exepect different parameters. By learning IE's attachEvent, I found no way to pass parameters. ...
5
by: Bert | last post by:
Hello, I'm having some problems with creating a script that works on both Mozilla browsers as IE. I want to change the background color of textareas and input text fields as soon as somebody...
18
by: luco | last post by:
Hi! I'm having a problem with attachEvent function. I'd like to add attachEvent dynamically to some objects so that each could execute event function with different parameter value. The question...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
3
by: SkyZhao | last post by:
i use attachEvent to bind a event ; code: img.attachEvent("onclick",alert("aa")); div.appendChild(img); div.innerHTML+="some text"; the event can not work; why can't i use it? if i use...
6
by: SkyZhao | last post by:
if i use AttachEvent like this,it can't work; eg: var img = document.createElement("img"); img.attachEvent("onclick",alert("test")); var div = document.createElement("div");...
12
by: Max | last post by:
Hi All, I need to check if attachEvent was done to an element. The problem is that attachEvent does not save this information anywhere. Is there any way to do this??? Thanks, Max
17
by: dmorand | last post by:
I'm trying to get attachEvent to work for IE, but am having a problem. It calls hello() the first time, but then the script errors out. Should hello() be returning something to the attachEvent...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.