seanrs97 seanrs97 - 6 months ago 20
HTML Question

How can I get the value of an option without the need to click a button?

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.

Answer

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 even on buttons. Instead you just need to work with another event.