By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

IE changing an onclick event dynamically

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.