Reputation: 333
Is it possible to read in the value of an option box without the need to click a button first for the value to be identified? I'm working on a JavaScript game at the moment, where the user selects what fish they want to draw and depending on what fish they have selected, the corresponding fish will be drawn onto the canvas, when the user clicks anywhere on the canvas. Iv'e tried numerous methods, but none seem to work for me without the need of a button being clicked first. Any help is much appreciated. Thanks.
Upvotes: 0
Views: 110
Reputation: 1060
You just need to get the value of the selected option when the user touches the canvas.
function canvasTouchEventHandler() {
//get selected option and do stuff
}
Getting the value when you click a button does the exact same thing- an event is dispatched which has a handler that gets the value. You're probably used to working with the click
event on buttons. Instead you just need to work with another event.
Upvotes: 3
Reputation: 7614
You can always start your draw()
function as the window loads.
Assuming your canvas drawing function name is draw()
, you can use the code below:
window.onload=function(){draw()};
If you are specifically referring to redrawing the canvas every time the user selects a new option, you can opt for a simple onchange
solution in your HTML elements like this:
<option onchange="draw()">
<!--Values / Selects etc here-->
</option>
Upvotes: 1