html - JavaScript fireEvent not a function? -
html - JavaScript fireEvent not a function? -
i've been trying create game battleship in javascript school assignment, , i'm stuck trying create opponent ai. i've created event when click cell in grids:
function addlistener(evt) { evt.addeventlistener('click', function(){ //bunch of code }); }
now run function addlistener
every time create new cell in grid in nested loop:
yourcell.setattribute('id', evt + string(a) + string(b)); addlistener(yourcell);
what want opponent run click event when i've made turn, wrote test out fireevent
function:
function enemyturn() { document.getelementbyid('yourgrid00').fireevent('onclick'); }
according sec code example, set id of cell 'yourgrid00'
, i've confirmed inspecting html code after javascript code has been run, , function enemyturn()
never run before each cell has been created , assigned id.
what not understand next error on fireevent
line:
uncaught typeerror: undefined not function.
does know i'm doing wrong?
fireevent
old method supported on net explorer versions 8 , lower. should instead utilize dispatchevent
if want handle up-to-date browsers.
dispatches event
@ specified eventtarget
, invoking affected eventlisteners
in appropriate order. normal event processing rules (including capturing , optional bubbling phase) apply events dispatched manually dispatchevent()
.
you have create event
manually though - can't phone call dispatchevent('onclick')
. i've pulled first bit of next code this answer.
var evt = document.createevent("mouseevents"); evt.initmouseevent("click", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); function enemyturn() { document.getelementbyid('yourgrid00').dispatchevent(evt); }
jsfiddle demo.
javascript html css
Comments
Post a Comment