Jump to content


changing onclick attribute dynamically,

  • Please log in to reply
1 reply to this topic

#1 GBS

  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 04 May 2006 - 02:01 PM

Hi all,,

I had a real problem since few days,, about changing mouse attributes dynamically, using javascript,
To explain, using a line of code like:
just worked fine using Firefox & Opera, but it didn't worked using IE...

Trying to understand & solve the problem, I've put in a debug window the onclick attribute content, & result under IE was looking like:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
function anonymous()
& under FF or Opera, it was just like:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
So, the problem was about the onclick format function,, which is under IE: function anonymous() {.......}

Finally, to dynamically change the onclick attribute, I've used that script which detects if the browser has the keyword 'function' in the onclick attribute or not,, & changes in the correct format the attribute,
<title>changing mouse attributes,</title>

function filterLineBreaks (str) {
str = str.replace(/\r\n|\r|\n/g, ' ');
return str;

function ChangeAttribute(node)
// using:
// var str=document.getElementById('debug').getAttribute("onclick");
// makes a bug with IE, because of the line breaks,
// so, we get the onclick attribute using the window debug value

var str=document.getElementById('debug').value;
result = filterLineBreaks(str);

//searching for the keyword 'function' in the onclick attribute
result = str.search("function");

if (result!=-1) // the keyword 'function' has been found in the onclick attribute, meaning 'the browser is like IE'
    //works with IE, but not with Opera or FF:
    var func="function anonymous() {AttributChanged(this);}";

    // BTW, that one didn't work,, don't know why,... :
    // node.setAttribute("onclick","function anonymous() {AttributChanged(this);}");
    //works with FF & Opera but not with IE !
alert('nice shot,,... now we try to change the onclick attribute,,');

//works for all
node.value="value changed,... click again to test,,";

//debuging the new onclick attribute

function AttributChanged(node)
alert('well done,, test finished! :)');

function debug()

<input type="button" id="testing" onclick="ChangeAttribute(this);" value="testing,.... click on me,,">
<textarea id="debug" cols="100" rows="10"></textarea>

// debug the current onclick attribute on bodyload

I know,,... I could have done this by:
-detect user agent
-change the onclick attribute depending of the agent,
But I wanted to know also how to remove the break lines in a texarea value,... :)

Hoping it will help some,

Sorry for my english,... I do my best, but sometimes,...
1st intention: understand the problem
2nd intention: try to solve it
3rd intention: try to understand how to solve it :)

#2 eemerge

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 02 June 2006 - 11:18 AM

just helped one :)
had some trouble fixing this on IE/Firefox, to make it work on both of them....of course, the initial code worked fine in FIrefox and Opera...IE sucked as usual...

Thanks for the tips...saved me a lot of 'researching' :)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users