user1508174
user1508174

Reputation: 49

Javascript Code does nothing when executed

I'm attempting to build an incremental game, but NONe of my javascript code is working, whether I access it locally or upload it to a webhost.

The HTML is as follows:

<script src="game.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
 <h1>Incremental!</h1>

<button id="click" type="button" class="btn btn-primary btn-lg btn-block">CLICK ME</button>
<hr>
<p>Clicks: <span id="total_clicks">0</span>
</p>
<p>Auto Clicks Per Second: <span id="autoClickers">0</span>
</p>
<hr>
<button id="autoClickerBuy" class="btn btn-info btn-block">BUY AN AUTOCLICKER</button>
<!--ADDED--><button id="upgradeTwoClicks" class="btn btn-danger btn-block">TWO CLICKS PER CLICK</button>
<!--ADDED--><button id="upgradeTwoAutoClicks" class="btn btn-danger btn-block">TWO CLICKS PER AUTOCLICKER</button></div>

And the Javascript file(game.js) is as follows:

var totalClicks = 0;

$('#click').click(function(){
totalClicks++;
document.getElementById("total_clicks").innerHTML = totalClicks;
});

I cannot figure out why it isn't working...it's literally a copy and paste from a tutorial.

Upvotes: 0

Views: 116

Answers (4)

Jag
Jag

Reputation: 169

Try this link: http://jsfiddle.net/VDV6L/ you missed this
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

Upvotes: 0

user2864740
user2864740

Reputation: 61865

Scripts that appear initial in the HTML are executed sequentially, in order.

The $ function, from jQuery, is not found because the game code (game.js) is run before jQuery library has been loaded. Change the order such that:

<!-- dependencies first -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- dependents -->
<script src="game.js"></script>

Scripts can also be loaded "async", but such is not relevant here.

Upvotes: 4

Devendra Soni
Devendra Soni

Reputation: 1934

i have create a jsfiddle using your code and its working fine for me see the link of jsfiddle.

i think you have not included jQuery file in your html page.

code:-

var totalClicks = 0;

$('#click').click(function(){
totalClicks++;
document.getElementById("total_clicks").innerHTML = totalClicks;
});

link- http://jsfiddle.net/XUjAH/1092/

thanks

Upvotes: 0

Diomedes
Diomedes

Reputation: 125

open develop tool of your browser(firebug in firefox, for example) and watch console errors... But reading your code... have you include jquery library?

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

Upvotes: 0

Related Questions