Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8879095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:52:31+00:00 2026-06-14T19:52:31+00:00

I have HTML select element being populated via a KnockoutJS viewmodel like so: <select

  • 0

I have HTML select element being populated via a KnockoutJS viewmodel like so:

<select data-bind="options: $data.answers,  optionsText: function(item) { return item.text }, value: selectedAnswer, optionsCaption: 'Choose...'"></select>

What I would like to do is set the class attribute of each option element by some means, so that I end up with:

<option class="someValue">12345</option>

Can this be done in KnockoutJS? I’m having difficulty in finding a solution for this.

EDIT:

I’ve just tried the foreach binding, as suggested by Artem and it is very close to working as I want. But there is one issue.

On the Question object there is a function which subscribes to the SelectedAnswer observable:

this.selectedAnswer.subscribe(function (answer) {}

When I use the foreach binding, the subscribe() fires off, once for each question (it shouldn’t fire until I’ve chosen an answer. I think this is because the “Choose…” option is now not shown).

For the foreach binding, how can I set the default text back to “Choose…” and stop the selectedAnswer.subscribe() from firing until the user has chosen an item, and not when the list is being populated.

EDIT:

OK here’s how I’ve done this.

In the KO viewmodel class I have a boolean bindingFinished which I check inside of selectedAnswer.subscribe(). If false then we just return from the function; if true then carry on as normal.

Also I add a default “Choose” option to the answers by adding a new Answer to the beginning of the array. The end result is that the subscribe function is not executed when the data is being bound, only after the user has chosen an option.

Many thanks to Artem for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:52:32+00:00Added an answer on June 14, 2026 at 7:52 pm

    You can use foreach binding instead of options in the select tag:

    <select data-bind="value: selectedAnswer">
        <option class="someValue">Choose...</option>
        <!-- ko foreach: $data.answers -->
        <option class="someValue" data-bind="value: $data, text: text"></option>
        <!-- /ko -->
    </select>
    

    Here is working fiddle: http://jsfiddle.net/vyshniakov/5bPWQ/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a html element (like select box input field) in a table. Now
I have a html combo/select. When the user selects a element, I want to
i have a html select with options in my views . onchange i want
I have problem while loading data into html select when users press or click
I have an HTML select element created in PHP. I want to add $car
I have a select element in the form with different options. On property of
OK, I have two HTML select elements and a button. I need to disable
I have n number of select elements in an html page that are used
I have a html select list menu,when user select an option he/she is redirected
I have a classic HTML select box: Show: <select name=show id=showThreads> <option value=all selected=selected>All</option>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.