...

View Full Version : Problems with Changing Onclick events on Buttons.



xfinnerjx
06-10-2010, 07:37 PM
Hi all,

I hope that someone can help me with this strange problem I have here.

I have some script which changes a button and changes the onclick event.

The strange thing is that although it is changing it. All the events have the same parameter even though my code is giving each button's onclick event it's own unique parameter.

Here's my Code.

var AllocPeople = window.opener.document.getElementById('AllocPeople');

if(AllocPeople.value.length > 0)
{
var people = AllocPeople.value.split(",");
var Controls = new Array();
for(var P in people)
{
var id = people[P];
Controls[P] = document.getElementById('bt_AP_'+id);
Controls[P].innerHTML = "Deallocate";
Controls[P].onclick = function() {Remove_Player(id)};
}

}


Thanking you all in advance.

xfinnerjx
06-10-2010, 09:34 PM
Solved this little problem I was having.

I've had to make the button changes happen within a separate function to get the required result.

so my code now look like this:-

function bt_test(id)
{
var bt = document.getElementById('bt_AP_'+id);
bt.innerHTML = "Deallocate";
bt.onclick = function() {Remove_Player(id)};
}
var AllocPeople = window.opener.document.getElementById('AllocPeople');

if(AllocPeople.value.length > 0)
{
var people = AllocPeople.value.split(",");
var Controls = new Array();
var ids = new Array();
for(var P in people)
{
bt_test(people[P]);
}

}


As this now works I would really like to know why this works when all I have done is seperate the code into functions. When the over all logic is the same.

Hope someone can explain this to me.

Thank you all in advance.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum