0

So I'm creating a form for my incremental game which allows you to choose how many protons, neutrons, and electrons are in an atom. The code for selecting how many protons is here:

<select class="form-control" id="protons_in_atom">
<option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
</select>

I also have a button:

<button id="atom_creator" type="submit" class="btn btn-primary">Create your atom!</button>

On the click, it executes these lines of code:

document.getElementById("atom_creator").onclick = function () {
    player.temp_protons_in_atom = document.getElementById("protons_in_atom");
    return alert(player.temp_protons_in_atom);
};

Unfortunately, what I receive is: [object HTMLSelectElement]
How do I cause player.temp_protons_in_atom to equal the option chosen?

Jonathan Spirit
  • 569
  • 3
  • 7
  • 14

2 Answers2

1

Check this link out. It details how to get the value of the selected option.

 document.getElementById("atom_creator").onclick = function () {
     temp = document.getElementById("protons_in_atom");
     player.temp_protons_in_atom = temp.options[temp.selectedIndex].text;

 };
Community
  • 1
  • 1
Harry
  • 394
  • 2
  • 10
0

This

player.temp_protons_in_atom

is the select/dropbox itself, to get the selected opinion use this:

return alert(player.temp_protons_in_atom.options[player.temp_protons_in_atom.selectedIndex].text);

Refrence is this answear.

Community
  • 1
  • 1
hultberg
  • 126
  • 1
  • 8