473,545 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically adding javascrip with javascript

Is it possible to use javascript to add javascript to a DOM object?
I've the following code to add an image to a page:

function flagError(id){
var image = document.create Element('img');
image.alt = "error";
image.src = "./resources/errorNew.gif";
var uncle = getUncle(id);
if(!first_child (uncle)){
uncle.appendChi ld(image);
}
}

what I want to do is use wz_tooltip to add a nice tooltip to the image.
The end result would be something like this:

<img src="./resources/errorNew.gif" alt="error" onmouseover="re turn
escape('Repitio n.');"/>

I've looked at doing it via:

image.onmouseov er = "\"return escape('Repitio n.');\"";

but that doesn't seem to work at all... any ideas anyone?

Cheers,

Dom

Jan 17 '06 #1
7 3439
an***********@g mail.com said the following on 1/17/2006 9:58 AM:
Is it possible to use javascript to add javascript to a DOM object?
I've the following code to add an image to a page:

function flagError(id){
var image = document.create Element('img');
image.alt = "error";
image.src = "./resources/errorNew.gif";
var uncle = getUncle(id);
if(!first_child (uncle)){
uncle.appendChi ld(image);
}
}

what I want to do is use wz_tooltip to add a nice tooltip to the image.
The end result would be something like this:

<img src="./resources/errorNew.gif" alt="error" onmouseover="re turn
escape('Repitio n.');"/>

I've looked at doing it via:

image.onmouseov er = "\"return escape('Repitio n.');\"";


image would be my last choice of a variable name for an image.

imageRef.onmous eover = function(){
return escape('Repitio n.')
}

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 17 '06 #2
> I've looked at doing it via:
image.onmouseov er = "\"return escape('Repitio n.');\"";


Try this instead:

image.onmouseov er = new Function("retur n escape('Repitio n')");

Jan 17 '06 #3
Ronaldo Junior said the following on 1/17/2006 11:50 AM:
I've looked at doing it via:
image.onmouse over = "\"return escape('Repitio n.');\"";

Try this instead:

image.onmouseov er = new Function("retur n escape('Repitio n')");


I actually just learned that whether you use function() or new Function
in that position depends on what scope you want it to have.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 17 '06 #4
> I actually just learned that whether you use function() or new Function
in that position depends on what scope you want it to have.


Would you care to explain? :)

Jan 17 '06 #5
an***********@g mail.com wrote:
Is it possible to use javascript to add javascript to a DOM object?
In a sense, yes.
[...]
The end result would be something like this:

<img src="./resources/errorNew.gif" alt="error" onmouseover="re turn
escape('Repitio n.');"/>

I've looked at doing it via:

image.onmouseov er = "\"return escape('Repitio n.');\"";

but that doesn't seem to work at all... any ideas anyone?


If you use

<img src="resources/errorNew.gif" alt="error"
onmouseover="re turn escape('Repitio n.');"
id="foo"/>

then (supported by UAs conforming to W3C-DOM Level 2 Events here, so not IE)

document.images['foo'].addEventListen er(
'mouseover',
function()
{
return escape('Repitio n.');
},
false);

or (supported by all widely distributed Web browsers, including IE)

document.images['foo'].onmouseover = function()
{
return escape('Repitio n.');
}

or (IE-only)

document.images['foo'].attachEvent(
'onmouseover',
function()
{
return escape('Repitio n.');
});

will add an event listener (or overwrite the previous one on assignment)
to the element represented by the respective DOM element object.

[However, there is no need for escape() in either example.]

RTFM and do some research on previous discussions before you post;
this is a _news_group.

<URL:http://jibbering.com/faq/>
PointedEars
Jan 17 '06 #6
"Ronaldo Junior" <se******@gmail .com> writes:
I actually just learned that whether you use function() or new Function
in that position depends on what scope you want it to have.


Would you care to explain? :)


When you write
function () { return foo; }
the "foo" variable refers to the closest enclosing declaration of
a variable called foo. So, inside another function, e.g., :

function setHandler(elem , foo) {
elem.onclick = function(){ alert(foo); };
}

the inner "foo" refers to the local variable created by the outer
function argument. I.e., it works to write:
setHandler(docu ment.body, "YOU TOUCHED THE BODY");

When you create a function using the Function method (used as a
constructor or not), the body of the function will not see surrounding
variables, but only the global scope (and parameters of the created
function ofcourse). So, if you do:

var foo = "GOTCHA!";
function setHandler2(ele m, foo) {
elem.onclick = new Function("alert (foo);");
}

then calling
setHandler2(doc ument.body, "YOU TOUCHED THE BODY");
and clicking the document body will alert "GOTCHA!", not the intended
message.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 18 '06 #7
Sorry Thomas, I checked the FAQ and searched previous posts before
posting but I guess my google skills - or ability to grok the answers -
aren't as top-hole as yours. Thank you all for the help.
Cheers, Dom

Jan 18 '06 #8

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

Similar topics

5
4884
by: john_williams_800 | last post by:
Hi; I am just starting to use the DOM to do some more advanced javascripting so please be patient with my question if it is an ignorant question. I would like to use the DOM to dynamically create an html table via javascript. While that table is being dynamically created in a javascript function I would like to dynamically insert text...
5
3715
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button. When the user clicks the "Add another email" it will call a client side JavaScript function, add_email, which will dynamically add a new set of...
3
1792
by: Brett Wesoloski | last post by:
I can not seem to figure this out, but really haven't work a lot with javascrip and .NET. I have a data grid and I want to put a button in it and when the button is pressed it will call my javascript. Now the grid is in a .ascx file. The other kicker is I want to also use the databinder property to get a value as the grid is loading and...
1
1510
by: hellohi | last post by:
Hello Everyone, I wanted to change the existing variable names dynamically.( like www.aol.com ) in this site images and some text is changing dynamically. like wise i need javascrip to write Thanks in advance
5
3198
by: Liquidtouch | last post by:
I'm not much of a HTML or Javascript programmer but have a little experience just hacking away at it. I am creating a HTML application and would like to be able to add or remove rows of a table. The row that gets added will need to contain 2 columns, 1 which contains an IFRAME, and the other will contain a Javascript Menu which will be used...
7
4853
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio buttons and check box. I am adding two radio buttons once for a row.Now my problem is the radio buttons in all the rows that added dynamically are...
1
2143
by: Shraddha | last post by:
Hi, I am adding some ASP.Net user controls (.ascx file) dynamically on the button click. The user control will get added as many times userhits the button. Now on the click of the submit button, I want to do some form level validations. For example suppose my .ascx file contains a text box with id say "trialTextBox". Now if user hits the...
4
4465
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is selected, the row is added using JavaScript. The script uses cloneNode to clone a hidden template row and all of its children and then adds the new...
9
2515
by: DL | last post by:
That is, for an iframe with onload attribute to preset a width and height then depending on the length of its content to auto-expand instead of scrolling. The following URL is quite interesting, http://www.phpmix.org/iframe_height_auto_resize, however, it failed to work for me, OS = XP w/ sp2; brower = IE7, but my app is quite complicated......
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.