Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

That title is a mouthful... Hey people! I've been having this bug with a web app project for school that I haven't been able to get passed after trying alternate methods for the past week or so. I'm hoping someone might be able to catch something that I can't...

Okay so here's the code which I believe is the source of the problem:

 echo"<div class='RightSidebar'> <button id='AtkBtn' type='button' onclick='attack(".$currentTurn.");'><b>Attack</b></button></a>\n";

Here's the Javascript function:

function attack(turn)
{
    if(turn == 1 )
        {alert('It is not your turn!!');}
    else
    {
        document.getElementById.("TextLog").innerHTML = "You attack the enemy!";
    }
}

(This is the only code in a file named "BattleMethods.js"; the script tags above the opening body tag states we'll be using this file. I can copy that line too if it helps...) No effects are being passed yet (i.e., the effect on the enemy's health). I want to get this running first and then code that out.

Now with that given information I need the following to happen:

1) Player hits the Attack button within a right sidebar div

2) TextLog div below the battle screen div that shows the enemy reveals current relevant information (i.e: "You attack the enemy!")

3) Client-side variables update appropriately (userHP, currentTurn, etc)

That is all. I can get the variables to update with no problem if I can figure out whats not allowing the function to react when player clicks the Attack button. I had done something similar in the item store and it worked fine. So I'm really at a loss. I hope this is enough information! Any help is much appreciated.

HTML where script lies:

echo"<!DOCTYPE html>\n";
echo"<html>\n";
echo"<head>\n";
echo"<title>SlateKeeper -- BATTLEROOM</title>\n";
echo"<meta name='viewport' content='minimum-scale=0.98; maximum-scale=5;initial-            scale=0.98;user-scalable=no;width=1024'>\n";

echo"<link rel='stylesheet' type = 'text/css' href='CSS/battleTowersStyles.css'>\n";
echo"<script language= 'Javascript' type = 'text/javascript' src='Javascript/BattleMethods.js'></script>\n";

echo"<style>\n";

echo"                body {background-color: #3b4870}\n";
echo"</style>\n";

echo"</head>\n";
share|improve this question
1  
"script tags above the opening body tag" - you mean inside it, or inside the <head>? While it shouldn't matter, placing them as children of <html> is invalid. –  Bergi Apr 19 at 18:31
    
What is the value of $currentTurn? Please post the HTML that the PHP produces. –  Bergi Apr 19 at 18:32
    
if($enemySPD < $yourSPD){$currentTurn=0;} else{$currentTurn=1;} Where $enemySPD is the speed of the enemy from the database (SQL) and $yourSPD is the users. In this case, the enemy's speed was 5 and the user's 10 making the $currentTurn = 0. –  Joshua Jay Espinosa Apr 19 at 18:36
    
OK, that looks fine (and yes you really should delete that comment and only edit it into the question) –  Bergi Apr 19 at 18:45

1 Answer 1

up vote 0 down vote accepted

Please see the following changes I've made to this code.It is working properly.
These are the javascript changes. function attack(turn) { if(turn == 1 ) { alert('It is not your turn!!'); } else { document.getElementById('TextLog').innerHTML = "You attack the enemy!"; } }
PHP CODE IS
$turn = 0; echo"<div class='RightSidebar'> <button id='AtkBtn' type='button' onclick='attack($turn)'><b>Attack</b></button></a>\n";


You had made mistake in the line,
document.getElementById.("TextLog").innerHTML = "You attack the enemy!";
It should have been

document.getElementById("TextLog").innerHTML = "You attack the enemy!";

share|improve this answer
    
Oh wow!!!! I can't believe I missed that. Thank you so much!! You're the best!!! –  Joshua Jay Espinosa Apr 19 at 19:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.