I am new to Javascript and working on exercises. I am just trying to use the onclick and when the FIGHT button is clicked, then the fight function runs. For some reason the fight function is not running but I am able to get the button through DOM.
HTML
<!doctype html>
<html>
<head>
<title>Goal4: Assignment - Duel III</title>
<link rel="stylesheet" href="css/style.css">
</head>
<script src="js/main.js"></script>
<body>
<div id="scores">
<div id="kabal">
<p></p>
</div>
<div id="kratos">
<p></p>
</div>
<div class="clear"></div>
</div>
<div id="fight_box">
<div id="fight_btn">
<h4 id="round">Click To Start Fight</h4>
<a href="#" class="buttonblue">FIGHT!</a>
</div>
<div id="fight_bg">
<img src="images/fight_bg.png">
</div>
</div>
<script src="js/main.js"></script>
</body>
</html>
Javascript
/*
Name: Walker Kinne
Date: 12/1/13
Assignment: Goal1: Assignment: Duel1
*/
(function(){
console.log('program starts');
var button = document.getElementsByTagName('a');
console.log(button);
button.onclick = function(e){
fight();
e.preventDefault();
return false;
};
function fight(){
console.log("in the fight function");
var fighter1 = {name:'Spiderman', damage:20, health: 100};
var fighter2 = {name:'Batman', damage:20, health: 100};
var minDamage1 = fighter1.damage * .5;
var minDamage2 = fighter2.damage * .5;
var f1 = Math.floor(Math.random()*(fighter1.damage - minDamage1)+minDamage1);
var f2 = Math.floor(Math.random()*(fighter2.damage - minDamage2)+minDamage2);
console.log(fighter1.name);
// console.log(f1);
//console.log(f2);
fighter1.health -=f2;
fighter2.health -=f1;
console.log("player1:" + fighter1.health);
console.log("player2:" + fighter2.health);
var a = fighter1.health;
};
//console.log(playerOneName+":"+playerOneHealth+playerTwoName+":"+playerTwoHealth);
//var results = winnerCheck();
//console.log(results);
/*if(results === "no winner"){
document.getElementById("kabal").
//alert(fighter1[0]+":"+fighter1[2]+" ROUND"+round+" OVER"+fighter2[0]+":"+fighter2[2]);
}else{
alert(results);
break;
}
*/
/* End of fight function */
/*
function winnerCheck(){
console.log("winner check");
var result = "no winner";
if(fighter1[2]<1 && fighter2[2]<1){
result = "You Both Die";
}else if(fighter1[2]<1){
result= fighter2[0] + " wins!!";
}else if(fighter2[2]<1){
result = fighter1[0] + " wins!!";
};
return result;
};
\/******** Program Starts ********/
})();
fightis just fine where it is. A) it is hoisted to the top of the scope, and B) it's not evaluated until theonclickhandler is invoked, some point in the future.