473,439 Members | 5,051 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,439 software developers and data experts.

Events in the createElement function.

Hello, I'm trying to make a navigation bar and need to assign
mouseover, mouseclick etc. events to a img tag dynamically, here's what
im using:

function getP_Element(p_imgsrc,p_id)
{var tuf = document.createElement("img");
tuf.src=p_imgsrc;
tuf.id=p_id;
tuf.onmouseover="alert('It works!')"
return(tuf);
}

would there need to be some other path like tuf.events.onmouseover?

Jan 15 '06 #1
17 11475
or just generally how would you create:

<img src="[image source]" onmouseover="[javascript]" id="img1" />

Jan 15 '06 #2


mo********@gmail.com wrote:

{var tuf = document.createElement("img");
tuf.src=p_imgsrc;
tuf.id=p_id;
tuf.onmouseover="alert('It works!')"


tuf.onmouseover = function (evt) {
alert('It works!');
};
is one way to do that, probably the best cross browser way when
scripting HTML document.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 15 '06 #3
oooh ok got it, thank you so much!

Jan 15 '06 #4
mo********@gmail.com said the following on 1/15/2006 1:51 PM:
Hello, I'm trying to make a navigation bar and need to assign
mouseover, mouseclick etc. events to a img tag dynamically, here's what
im using:

function getP_Element(p_imgsrc,p_id)
{var tuf = document.createElement("img");
tuf.src=p_imgsrc;
tuf.id=p_id;
tuf.onmouseover="alert('It works!')"


tuf.onmouseover = new Function('alert("It works")')

Or:

tuf.onmouseover = someFunctionToExecute;

Note the abscence of the () on the someFunctionToExecute

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 15 '06 #5
mo********@gmail.com wrote:
tuf.onmouseover="alert('It works!')"


tuf.onmouseover = function() { alert('it works!'); }

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 15 '06 #6
Martin Honnen said the following on 1/15/2006 2:13 PM:


mo********@gmail.com wrote:

{var tuf = document.createElement("img");
tuf.src=p_imgsrc;
tuf.id=p_id;
tuf.onmouseover="alert('It works!')"

tuf.onmouseover = function (evt) {
alert('It works!');
};
is one way to do that, probably the best cross browser way when
scripting HTML document.


What are the advantages/disadvantages of using function (evt) over using
a new Function construct?

I have seen it done both ways and always wondered the differences and
advantages of each.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 15 '06 #7
On 15/01/2006 19:31, Randy Webb wrote:

[snip]
What are the advantages/disadvantages of using function (evt) over using
a new Function construct?


Beyond the (potential) differences in scope chain?

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 15 '06 #8
Michael Winter wrote:
On 15/01/2006 19:31, Randy Webb wrote:

[snip]
What are the advantages/disadvantages of using function (evt) over
using a new Function construct?

Beyond the (potential) differences in scope chain?


I'll save you some typing :-)

<URL:
http://groups.google.co.uk/group/com...7368f8df727b19

--
Rob
Jan 16 '06 #9
RobG said the following on 1/15/2006 7:33 PM:
Michael Winter wrote:
On 15/01/2006 19:31, Randy Webb wrote:

[snip]
What are the advantages/disadvantages of using function (evt) over
using a new Function construct?


Beyond the (potential) differences in scope chain?


I'll save you some typing :-)

<URL:
http://groups.google.co.uk/group/com...7368f8df727b19
>



Thank you both.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 16 '06 #10
There is a handy library behaviour.js to automate the allocation of
event functions to multiple elements:

http://bennolan.com/behaviour/

Of course, you can just write the for loops yourself, and save your
users from having to download the library file, but this does look
tidy.

Nic

Jan 16 '06 #11
drnicwilliams said the following on 1/16/2006 3:57 AM:
There is a handy library behaviour.js to automate the allocation of
event functions to multiple elements:

http://bennolan.com/behaviour/


Not sure I would trust a JS file from a site that can't even get the
errors out of its own site:

Error: element has no properties
Source File: http://bennolan.com/behaviour/behaviour.js
Line: 128

That is in Firefox.

In IE, there is a syntax error on the main page:

Line 129
Char: 7
Error: Object required
Code: 0
URL: http://bennolan.com/behavior

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 16 '06 #12


Randy Webb wrote:

What are the advantages/disadvantages of using function (evt) over using
a new Function construct?


A function expression
= function (arg) { function body }
is (in my view) the simpler and more natural way to write an expression
that evaluates to a function.
Constructing a function with the Function constructor
= new Function("arg", "string with function body code")
is only needed if you need to construct the parameter name and/or
function body code at runtime from a string. It is much like eval in
that at run time string with source code needs to be evaluated. If you
only use eval if really needed (e.g. you need to construct code at
runtime depending on user input) then you should only use new
Function("code") the same way.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 16 '06 #13
On 16/01/2006 00:33, RobG wrote:
Michael Winter wrote:
On 15/01/2006 19:31, Randy Webb wrote:
What are the advantages/disadvantages of using function (evt) over
using a new Function construct?
Beyond the (potential) differences in scope chain?


I'll save you some typing :-)


Thank you. That's very good of you. :-D
<URL:
http://groups.google.co.uk/group/com...7368f8df727b19
>


I thought I'd written about it before...

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 16 '06 #14
I know this isnt teribly related but is the lack of () becuase when you
assign it you're using the source code of that function, the plain
function name used as a varible holds the source code for the function
no?

Jan 17 '06 #15
mo********@gmail.com said the following on 1/16/2006 6:52 PM:

Please quote what you are replying to.

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.
I know this isnt teribly related but is the lack of () becuase when you
assign it you're using the source code of that function, the plain
function name used as a varible holds the source code for the function
no?


Are you referring to when it is defined you leave off the ()?

Something like this:

window.onload = someFunction;

If so, it assigns a function reference to the onload event. If you add
the () then it assigns the results of that function to the onload.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 17 '06 #16
I can't find the button! Also thanks for the info on function refrences.

Jan 18 '06 #17
mo********@gmail.com said the following on 1/17/2006 7:40 PM:
I can't find the button! Also thanks for the info on function refrences.


It's there Mr. Spock, just find it! :)

Hint: It's a link, not a button.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 18 '06 #18

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

Similar topics

2
by: DaRik | last post by:
Hi, I'm having some difficulties with a menu I'm making. I build up the menu through DOM. I append childnodes to a tree. 2 types of children are possible: url (a hyperlink) and sub (a submap). ...
3
by: Derek Basch | last post by:
Is it bad form to use the global window variable to reference an event handlers window? Like so: function SortableTable() { oFilterAdd = this.document.createElement("button");...
6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
1
by: Cy | last post by:
This works wonderfully in Firefox, but when it runs for the first time in IE it only creates the field, but won't create any attributes for the events. When debugging the events are null. How can...
2
by: Steve Macleod | last post by:
Hi, I was wondering if anyone would be good enough to have a look at the following code and tell me if there is something im missing! drug_list is an array of drug details table is a reference to...
4
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
5
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.