Forum Moderators: open

Message Too Old, No Replies

Change <input> to <a> maintain onclick event

OK, cant get this working in IE

         

willis1480

1:56 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



Ive been beating my head against the wall for many hours now and have gotten nowhere. Go figure.

Here is the problem (simplified):
Want to change <input type='button' value='click me' onclick='myFunc(param)' id='btn_1'>
to <a href='#' onclick='myFunc(param)'>click me</a>

here is the javascript code to try and do this:
//GET THE INPUT
iObj = document.getElementById('btn_1');
var curEvent = iObj.getAttribute('onclick');
//or curEvent = iObj.onclick;

//CREATE A AND ADD INFO
var a1 = document.createElement("a");
a1.appendChild(document.createTextNode(iObj.value));
iObj = iObj.parentNode.replaceChild(a1, iObj);
iObj.setAttribute('onclick',function(){eval(curEvent);});
//or iObj.onclick = function(){eval(curEvent);};

works fine in FF, Google, etc...but not IE

Ive tried adding the onclick event to the <A> before replaceChild

If I just try and add an alert() or someother function it works fine. Seems to be an issue with replacing the onclick or something. Dont get any errors though.

Any help would be appreciated. I am attempting to implament CSS buttons on my site and dont want to go through each page and replace buttons and input tags.

Fotiman

2:34 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This worked for me:


//GET THE INPUT
iObj = document.getElementById('btn_1');
var curEvent = iObj.onclick;
//CREATE A AND ADD INFO
var a1 = document.createElement("a");
a1.appendChild(document.createTextNode(iObj.value));
a1.onclick = curEvent;
iObj = iObj.parentNode.replaceChild(a1, iObj);