0

Self teaching Node.js and Javascript stuff. I have in the form in my HTML and select, drop down menu option. How do I get the index of the selected value?

So far im trying this:

var e = req.body.boolean_choice;
boolChoice = e.selectedIndex;

I have the req.body working for getting the inputted values in a text box, but it tells me selectedIndex is not a thing. So then I tried:

var e = req.body.boolean_choice

to see what that gave and it just gave undefined.

Is there a way to do this?

Here is the HTML:

<form action="http://localhost:3000" method="POST">
Form:<br>
<br>
<br>
Text Box 1: <input type="text" name="tb1"  size="35" placeholder="&quot;@10SadioMane&quot; OR &quot;#Mane&quot;">
<br>
<br>
<select id="choice" name="choice">
  <option value="OR">OR</option>
  <option value="AND">AND</option>
</select>
<br>
<br>
<input type="submit" value="Submit">

2 Answers2

3

Form submitting will pass only value property of selection. Also use debugger in IDE that you use, to explore req.body, instead of trying to guess what's there.

Sergey Yarotskiy
  • 4,536
  • 2
  • 19
  • 27
  • 1
    or do `console.log(req.body)` to view it in the console running node – George May 05 '17 at 12:53
  • im not guessing at what is there, I've used `req.body.tb1` which returns the input of that text box. So I assumed the logic would be the same for this. – Dominic Reeve May 05 '17 at 12:59
0

Happy to help.

First of all get the Selected option DOM element

var el = document.querySelector('#choice option:selected');

ibn your case it will be req.body.querySelector('#choice option:selected');

Now logic is to iterate to the previous siblings in the chain till you reach to the top.

var index = 0; 
while( el.previousSibling ! == null){
el = el.previousSibling;
index +=1;
}
console.log(index);
Swastik Pareek
  • 161
  • 1
  • 8