Forum Moderators: open

Message Too Old, No Replies

code works in firefox but not in IE

         

lmlm

5:32 pm on Jan 7, 2009 (gmt 0)

10+ Year Member



Hi
First i will say i am new to html and JavaScript.

i have created my own webpage and got a problem when running it on IE (on firefox it works smoothly)

i have simplified my problem to the following code
the problem is with the call to alert (i.e does not show the alert win while FF does)
Any help will be mostly appreciated
Thanks in advance.

------ code ----

/*
this code is for generating 2 images "buttons"
i need the images to sit one next to each other
and when clicking run some function (i have changed the function to alert just to simplify)
the image button need to be created inside the function since the information for the called function is locally inside the javascript function
*/

<html>
<head>
<title>problematic code</title>
<script type="text/javascript">

function createAddRemove()
{
// start removeFromBasketId
var elem=document.createElement("input");
elem.setAttribute("type","image");
elem.setAttribute("src","img/BasketRemove.jpg");
elem.setAttribute("onclick","alert('remove')");
document.getElementById("removeFromBasketId").appendChild(elem);
// end removeFromBasketId

//start of addToBasketId
var elem=document.createElement("input");
elem.setAttribute("type","image");
elem.setAttribute("src","img/BasketAdd.jpg");
elem.setAttribute("onclick","alert('add')");
document.getElementById("addToBasketId").appendChild(elem);
}
</script>
</head>
<body>
<div>
<a id="addToBasketId"></a>
<a id="removeFromBasketId"></a>
<a href="javascript:createAddRemove();">start</a>
</div>
</body>
</html>

coopster

5:58 pm on Jan 7, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, lmlm.

Did you try using the object notation for the event handler as opposed to setAttribute?

elem.onclick = function () {alert('remove')};

lmlm

10:21 pm on Jan 7, 2009 (gmt 0)

10+ Year Member




Thanks Coopster
It is working now both in FF and IE
Sure a great welcome to this forum
Cheers