George Avramoiu

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromGeorge Avramoiu
ToMe
Subjectonclick problem IE6
Date1 July 2005 06:39
Hello there,


I am having some problems with Internet Explorer. Basically I have a form
(test1) with hidden inputs called prod[1], prod[2]... prod[n]. I wrote a
function which creates a list of this items and writes them as text in a
table, for every item I also created a link which onclick calls a function
to delete an item (see below).

Everything it's working ok in Firefox but I didn't managed to make it work
in IE. I click on that link and nothing happens, I also put an alert at the
begining of the stergeProdus function but nothing appears, it's like that
function it's never called.

A construction like:<a onClick='stergeProdus(1)'>Delete 1</a> works
perfectly.

Also I installed an DOM inspector for IE6 which shows me that "DOM way" the
link above looks just like the link for deleting prod 1 created with
document.createElement. My guess it's that IE creates the object, but for
somewhat reason it doesn't treat onclick right if the object was created
like that.

The function stergeProdus(x) deletes the item from the list.
function stergeProdus(x){
var nr_produse=document.forms.test1.nr_prod.value;
var nume_x='prod['+x+']';
if (document.getElementById('prod['+x+']'))
{document.forms.test1.removeChild(document.getElementById('prod['+x+']'));}
for (var i=x+1; i<=nr_produse; i++){
	var produs_modif=document.getElementById('prod['+i+']');
	produs_modif.id = 'prod['+(i-1)+']';
}

the link to delete:
var linksterge = document.createElement('a');
	linksterge.onclick = ('stergeProdus('+i+')');
//	linksterge.setAttribute('onclick','stergeProdus('+i+')');
	linksterge.setAttribute('href','#');
	linksterge.appendChild(document.createTextNode('Sterge'));

Can you please help me make it work ok in IE, please ?

Thanks,
George
FromMe
ToGeorge Avramoiu
SubjectRe: onclick problem IE6
Date2 July 2005 10:33
George,

> Everything it's working ok in Firefox but I didn't managed to make it work
> in IE.

How about the others? Does it work in Opera? How about Safari? Konqueror?
iCab?

It really bugs me when people only mention IE and Firefox - it's not 1997
and there are more than 2 major browsers.

I don't use either of those browsers myself. As an additional bonus, the
other browsers may be able to highlight a problem for you.

> document.createElement. My guess it's that IE creates the object, but for
> somewhat reason it doesn't treat onclick right if the object was created
> like that.

That should make no difference.

> function stergeProdus(x){

You seem to have missed the closing } (I assume you have included this in
the original, because otherwise it would not work in FF).

> var nr_produse=document.forms.test1.nr_prod.value;
> for (var i=x+1; i<=nr_produse; i++){

This is a string, not a number. parseInt first.

> var nume_x='prod['+x+']';

You are not using this anywhere in the code you sent me ...

>     linksterge.onclick = ('stergeProdus('+i+')');

That is invalid. you cannot assign a string to an event property and expect
it to work. You must assign a function. For what you are doing, I suggest
you use the new Function constructor so it can evaluate the string:
linksterge.onclick = new Function('stergeProdus('+i+');');

That should work cross browser. Including the other browsers that you
forgot to mention... ;)

(setAttribute using a string should also work in all DOM browsers except IE
- IE is stupid and cannot understand that)


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.