5 Replies - 489 Views - Last Post: 19 June 2018 - 05:27 PM

#1 genek   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 18-August 17

how to get selected option

Posted 19 June 2018 - 04:52 PM

I have list of items and there I hava select with some options from observable array. How can I get selected option when I click update button?
grades and courses are both observable arrays
<!-- ko foreach: grades -->
                    <tr>
                        <td><input type="number" name="gradeValue" step=0.5 min=2.0 max=5.0 value=2.0
                                             title="Value" data-bind="value: value" placeholder="Value"></td>
                        <td><select name="id" required data-bind="options: $parent.courses,
	                                                      optionsText: function(i) { return i.name(); },
	                                                      optionsValue: function(i) { return i.id(); },
	                                                      optionsCaption: 'Choose course',
	                                                      value: course.id ">
	
	                        </select></td>
                        <td><input type="date" data-bind="value: date" required></td>
                        <td><input type="submit" value="Delete" data-bind="click: $root.deleteGrade.bind($data, $index())">
                            <input type="submit" value="Update" data-bind="click: $root.updateGrade.bind($data, $index())"></td>
                    </tr>
                    <!-- /ko -->


Is This A Good Question/Topic? 0
  • +

Replies To: how to get selected option

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2178
  • View blog
  • Posts: 6,609
  • Joined: 15-January 14

Re: how to get selected option

Posted 19 June 2018 - 04:54 PM

If you're talking about doing that in vanilla Javascript, you get the select element and then it has properties for options (an array) and selectedIndex. So selectElement.options[selectElement.selectedIndex] is the one selected. Each option has a value property that you can use to get the value.
Was This Post Helpful? 0
  • +
  • -

#3 genek   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 18-August 17

Re: how to get selected option

Posted 19 June 2018 - 04:59 PM

I don't talk about vanilla. Don't know what it is. I'm talking about knockout. Select is filled with objects from observable array and I want to get that selected object.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2178
  • View blog
  • Posts: 6,609
  • Joined: 15-January 14

Re: how to get selected option

Posted 19 June 2018 - 05:02 PM

Vanilla Javascript is regular Javascript that the browser runs. It's not a framework, it's just regular everyday Javascript. I don't know what Knockout is, so if you're not using regular Javascript then you need to check the documentation for whatever you're using to figure out how they want you to do it.
Was This Post Helpful? 0
  • +
  • -

#5 genek   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 18-August 17

Re: how to get selected option

Posted 19 June 2018 - 05:10 PM

so how can I in regular js get selected item from select?
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2178
  • View blog
  • Posts: 6,609
  • Joined: 15-January 14

Re: how to get selected option

Posted 19 June 2018 - 05:27 PM

That's what I described in my first reply. Here's the reference for more information:

https://developer.mo...MLSelectElement
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1