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

IE changing an onclick event dynamically

The following is a very simple example of what I want to do take an
elements oncontextmenu and changing it dynamically onclick of that
same element. The code below will fail unless you change the line

document.getElementById('div1').setAttribute('onco ntextmenu',
someText);
to
document.getElementById('div1').setAttribute('onco ntextmenu',
function(){alert('World World');return false;});

<html>
<head>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
someText);
}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;"
oncontextmenu="alert('Hello World');return false;"Hello </div>
</body>
</html>

The string I generate in the example is what the function should say
so is there anyway to take that string and make a literal translation
in IE?

I posted a while back something similar because I thought there was a
problem with the setAttribute function, no real great answers were
found but after further inspection it turns out setAttribute works it
just won't translate a string to a function. PS only IE has problems
with this I've tested with 6+7

Apr 18 '07 #1
3 2995
Daz
On Apr 18, 9:54 pm, ICPooreMan <ShaunPo...@gmail.comwrote:
The following is a very simple example of what I want to do take an
elements oncontextmenu and changing it dynamically onclick of that
same element. The code below will fail unless you change the line

document.getElementById('div1').setAttribute('onco ntextmenu',
someText);
to
document.getElementById('div1').setAttribute('onco ntextmenu',
function(){alert('World World');return false;});

<html>
<head>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
someText);}

//-->
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;"
oncontextmenu="alert('Hello World');return false;"Hello </div>
</body>
</html>

The string I generate in the example is what the function should say
so is there anyway to take that string and make a literal translation
in IE?

I posted a while back something similar because I thought there was a
problem with the setAttribute function, no real great answers were
found but after further inspection it turns out setAttribute works it
just won't translate a string to a function. PS only IE has problems
with this I've tested with 6+7
Perhaps I am missing something here (as it's 3:32AM and I'm tired),
but oncontextmenu is not an attribute, it's an event. Shouldn't you be
adding it using the addEventHandler() method? Also, someText is a
string, and I believe it should be a function. Sorry for sounding
ignorant, but what are you actually trying to do? I am sure I have it
wrong.

Apr 19 '07 #2
On Apr 18, 10:36 pm, Daz <cutenfu...@gmail.comwrote:
On Apr 18, 9:54 pm, ICPooreMan <ShaunPo...@gmail.comwrote:
The following is a very simple example of what I want to do take an
elements oncontextmenu and changing it dynamically onclick of that
same element. The code below will fail unless you change the line
document.getElementById('div1').setAttribute('onco ntextmenu',
someText);
to
document.getElementById('div1').setAttribute('onco ntextmenu',
function(){alert('World World');return false;});
<html>
<head>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
someText);}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;"
oncontextmenu="alert('Hello World');return false;"Hello </div>
</body>
</html>
The string I generate in the example is what the function should say
so is there anyway to take that string and make a literal translation
in IE?
I posted a while back something similar because I thought there was a
problem with the setAttribute function, no real great answers were
found but after further inspection it turns out setAttribute works it
just won't translate a string to a function. PS only IE has problems
with this I've tested with 6+7

Perhaps I am missing something here (as it's 3:32AM and I'm tired),
but oncontextmenu is not an attribute, it's an event. Shouldn't you be
adding it using the addEventHandler() method? Also, someText is a
string, and I believe it should be a function. Sorry for sounding
ignorant, but what are you actually trying to do? I am sure I have it
wrong.

I've tried addEventHandler and it doesn't seem to work for the same
reason as above. I think you are missing what I'm asking what I'm
looking to do is dynamically change some variables in the
oncontextmenu event, in order to do that the way it's set up now I
need to convert it to a string so that I can replace out things I want
to change. However, once I convert it to a string it's no longer a
function and is useless in that context. But there's no way as far as
I can tell to turn it back to a function from a string and that's the
problem it seems what I want to do can't be done.

Apr 19 '07 #3
Daz
On Apr 19, 1:57 pm, ICPooreMan <ShaunPo...@gmail.comwrote:
On Apr 18, 10:36 pm, Daz <cutenfu...@gmail.comwrote:
On Apr 18, 9:54 pm, ICPooreMan <ShaunPo...@gmail.comwrote:
The following is a very simple example of what I want to do take an
elements oncontextmenu and changing it dynamically onclick of that
same element. The code below will fail unless you change the line
document.getElementById('div1').setAttribute('onco ntextmenu',
someText);
to
document.getElementById('div1').setAttribute('onco ntextmenu',
function(){alert('World World');return false;});
<html>
<head>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
someText);}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;"
oncontextmenu="alert('Hello World');return false;"Hello </div>
</body>
</html>
The string I generate in the example is what the function should say
so is there anyway to take that string and make a literal translation
in IE?
I posted a while back something similar because I thought there was a
problem with the setAttribute function, no real great answers were
found but after further inspection it turns out setAttribute works it
just won't translate a string to a function. PS only IE has problems
with this I've tested with 6+7
Perhaps I am missing something here (as it's 3:32AM and I'm tired),
but oncontextmenu is not an attribute, it's an event. Shouldn't you be
adding it using the addEventHandler() method? Also, someText is a
string, and I believe it should be a function. Sorry for sounding
ignorant, but what are you actually trying to do? I am sure I have it
wrong.

I've tried addEventHandler and it doesn't seem to work for the same
reason as above. I think you are missing what I'm asking what I'm
looking to do is dynamically change some variables in the
oncontextmenu event, in order to do that the way it's set up now I
need to convert it to a string so that I can replace out things I want
to change. However, once I convert it to a string it's no longer a
function and is useless in that context. But there's no way as far as
I can tell to turn it back to a function from a string and that's the
problem it seems what I want to do can't be done.
I'm still not entirely sure what you mean. I am sure you've already
seen this, but if not, please take a look at it:
http://msdn.microsoft.com/workshop/a...ontextmenu.asp

It looks like you can do what you want, but not my using standard
JavaScript. I would suggest you look into adding a hard coded event
handler, and making the function do all the dynamic stuff. I trust you
are aware that your web app won't work on any browser other than IE?

Apr 19 '07 #4

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

Similar topics

2
by: wjbell | last post by:
I have a piece of javascript I need to modify. Right now it changes a stylesheet in the document between style.css and no_indent.css. These are in the head of my document: <link rel=stylesheet...
2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
8
by: cool2005 | last post by:
I tried to dynamically add/clone a <tr> from an existing <tr> to a <table> but the problem is that I need to have a "onclick" event handler in the <tr>. I have tried following A. approach...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
2
by: anony | last post by:
Hi, I'm trying to dynamically add a button to my ASP.NET / VB page. I can't seem to figure out how to add a routine to the OnClick event. Any insight would be greatly appreciated! Thanks,...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
11
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
3
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.