1

I have a javascript file linked to index.html like below:

<script src='game.js' type='text/javascript'>
</script>

Assume that game.js contains:

 var speed = ...;

Along with some other content.

I have 3 buttons on the HTML page that when clicked I want to change the variable speed in the javascript. Once clicked I want all 3 buttons to be disabled or hidden until the reset button is clicked. Any idea how I go about this?

4
  • Sorry guys I should of stated I cant use jQuery as its for a University assignment jQuery not allowed. Also just to inform you im making a HTML 5 Canvas snake type game the buttons are to set the speed of the snake for difficulty. Commented Apr 5, 2011 at 19:35
  • @Mark - if it's for an assignment you should also tag as homework so that people don't provide a complete answer. That would be cheating. Commented Apr 5, 2011 at 19:40
  • @Tomalek - both are true. In this instance he should have tagged as homework and in all cases when it is for an assignment, one should tag [it] as homework. Commented Apr 5, 2011 at 20:02
  • In either case, I'm really surprised that he's doing HTML5 stuff at school. +1 for his university!! Commented Apr 5, 2011 at 20:19

5 Answers 5

1

Using pure HTML/JavaScript, here's what I would do:

<form name="form1">
  <span id="buttons">
    <input type="button" name="button1" value="Speed1"/>
    <input type="button" name="button2" value="Speed2"/>
    <input type="button" name="button3" value="Speed3"/>
  </span>
  <input name="reset" type="reset"/>
</form>
<script type="text/javascript">
  var speed, buttonsDiv=document.getElementById("buttons");
  for (var i=1; i<=3; i++) {
    var button = document.form1["button" + i];
    button.onclick = function() {
      speed = this.value;
      alert("OK: speed=" + speed);
      buttonsDiv.style.display = 'none';
    };
  }
  document.form1.reset.onclick = function() {
    speed = null;
    alert("Speed reset!");
    buttonsDiv.style.display = 'inline';
    return true;
  };
</script>

Here is a working example: http://jsfiddle.net/maerics/TnTuD/

Sign up to request clarification or add additional context in comments.

2 Comments

You forgot to wait until the DOM has rendered.
@Tomalak: right, I forgot that important detail, I suppose the contents of the script tag should be wrapped in window.onload = function() { ... };.
0

Create functions within your javascript files that attach to the click events of each button.

The functions would change the variable you want.

aButtonelement.addEventListener('click',functionToChangeVariable,false)

Comments

0

Include the following in your Javascript file:

function DisableButtons() {
   speed = 100;
   document.getElementById("btn_1").disabled = true;
   document.getElementById("btn_2").disabled = true;
   document.getElementById("btn_3").disabled = true;
}

function EnableButtons() {
   document.getElementById("btn_1").disabled = false;
   document.getElementById("btn_2").disabled = false;
   document.getElementById("btn_3").disabled = false;
}

In your HTML, assign the following onClick events:

<button onClick="DisableButtons();">Button 1</button>
<button onClick="DisableButtons();">Button 2</button>
<button onClick="DisableButtons();">Button 3</button>    
<button onClick="EnableButtons();">Reset</button>

4 Comments

onClick is not the best route. Event listeners should be used instead.
@tjameson: I agree, though it'll probably do for a homework assignment.
Ah, I missed the part about it being a homework assignment. Still, it's good practice.
Homework? Oh man, there should be some sort of flashing red light when questions are tagged with "homework". I completely missed that.
0

something like this? http://jsfiddle.net/qMRmn/

Basically, speed is a global variable, and clicking a button with the class set-speed class will set the speed to a new value, and disable all of the set-speed buttons. Clicking the reset button will re-enable them.

The code should be fairly self explanatory.

Comments

0

Easiest way, use jQuery.

$("#idofbutton").click(function () {
    // change variables here
});

Or you could register an event:

document.getElementById("idofbutton").addEventListener('click', function () {
    // change variables here
}, false);

Source

2 Comments

for backward xbrowser compat, you should add a FALSE as argument # 3
Good catch. I usually develop for HTML5 compatible browsers, so I generally don't bother with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.