IE7 getAttribute and setAttribute problem
Posted on 2007-04-04
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the value then put it back as the oncontextmenu attribute.
Here's an example page if you want to try it out...
<title> testing </title>
var someText = String(document.getElementById('div1').getAttribute('oncontextmenu'));
someText = someText.replace("Hello","World");
<div id="div1" onclick="doSomething();return false;" oncontextmenu="alert('Hello World');return false;"> Hello </div>
The problem is after the onclick event oncontextmenu no longer works. I've figured out that the reason is that I cast the value of oncontextmenu to a string. Without that cast setAttribute will work correctly. However without the cast the replace method will not work. So I either need a way to cast the variable back or do a replace without the cast.