|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
AddEventListener
Hey guys, I don't like it when this happens, you have a piece of code, it works perfectly, you copy it over and it stops working lol.
I'm trying to change the onclick event of an element, and it is not. It works fine with a button, however with the TD tag which is what I am trying to apply it to does not work. Code:
//orig code
var t = document.getElementById("setbut");
t.value = "Set Pieces";
t.onclick=null;
if (window.addEventListener) { // Mozilla, Netscape, Firefox
t.addEventListener('click', startGame, false);
}
else { // IE
t.attachEvent('onClick', startGame);
}
//end of orig
//now I move this code into a loop for the cells I need changing
//new code
t = document.getElementById(i+tmpA[bn]+ii);
t.onclick=null;
if (window.addEventListener) { // Mozilla, Netscape, Firefox
t.addEventListener('click', bombard, false);
if(i==10 && ii==10) alert(t.click); ;
}
else {
img.attachEvent('onClick', bombard);
}
//end new code
Now I know the right parti is being called, due to the alert in the first if statement, however it just shows "undefined". So can I not use this to change the onclick of a td?
__________________
~~==~~ Whoever said nothing is impossible never tried pushing a revolving door ~~==~~ |
|
#2
|
|||
|
|||
|
Oh, never mind, it is working, I was just expecting the change to show in firebug aswell
|
|
#3
|
|||
|
|||
|
haha. I would have suggested to wrap the tag with the <a> tag (sorta like applying CSS onmouseover & onmouseout to a form button)... but nevermind
__________________
Sir, a desire of knowledge is the natural feeling of mankind; and every human being, whose mind is not debauched, will be willing to give all that he has to get knowledge. |
|
#4
|
|||
|
|||
|
actually using this how would I tell it to use variables?
I tried t.addEventListener('click', bombard(this.id), false); But that doesnt work |
|
#5
|
|||
|
|||
|
if you use 'this' what does it use? I thought 'this' meant the current element doesn't matter what the ID or Name is.... but not sure.
|
|
#6
|
|||
|
|||
|
Well to start with the td is like this
<td id="1-1" onclick="thiscell(this.id)"> but then I click the link I need it to go to <td id="1-1" onclick="bombard(this.id)"> So that the id of the clicked cell is available to the bombard function. |
|
#7
|
|||
|
|||
|
I know you are better at JS/DOM than I am, but I will try to help the best I can. Not sure if my suggestions will be useful or not.
Can you post your bombard function? If it's like: function bombard(x) { } try to add: alert(x); to the top of it to see what is being passed to the function. |
|
#8
|
|||
|
|||
|
I get "undefined" if I do that.
|
|
#9
|
|||
|
|||
|
So in your single element you have both?:
onclick="thiscell(this.id); bombard(this.id);" and only 'thiscell()' works (correctly)? |
|
#10
|
|||
|
|||
|
no, when the page is loaded the element has an onclick="thiscell(this.id)"
But after a function is called I need it to change to onclick="bombard(this.id)" |
|
#11
|
|||
|
|||
|
In your bombard function set you element (x as I've referred to it previously) to this.id and erase the x from:
function bombard(x) { } so it becomes PHP Code:
What do you get with that? -- this isn't going to work, ignore this post... Last edited by jamestrowbridge : March 23rd, 2009 at 01:45 PM. Reason: nevermind |
|
#12
|
|||
|
|||
|
In your first post, should:
img.attachEvent('onClick', bombard); be: t.attachEvent('onClick', bombard); |
|
#13
|
|||
|
|||
|
Have a look here: This seems like that issue you are having, possibly solved? http://www.captain.at/howto-addeven...-parameters.php
|
|
#14
|
|||
|
|||
|
Forget that crap I just posted, give me 5 min.
|
|
#15
|
|||
|
|||
|
This will only fix it for everything besides IE - I'm still working on it though (EDIT: SEE PAGE 2)
Code:
//orig code
var t = document.getElementById("setbut");
t.value = "Set Pieces";
t.onclick=null;
if (window.addEventListener) { // Mozilla, Netscape, Firefox
t.addEventListener('click', startGame, false);
}
else { // IE
t.attachEvent('onClick', startGame);
}
//end of orig
//now I move this code into a loop for the cells I need changing
//new code
t = document.getElementById(i+tmpA[bn]+ii);
t.onclick=null;
if (window.addEventListener) { // Mozilla, Netscape, Firefox
//changed some stuff in the line below:
t.addEventListener('click', function() { bombard(i+tmpA[bn]+ii); }, false);
if(i==10 && ii==10) alert(t.click); ;
}
else {
img.attachEvent('onClick', bombard);
}
//end new code
Last edited by jamestrowbridge : March 23rd, 2009 at 03:52 PM. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > AddEventListener |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|