DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Microservices With Apache Camel and Quarkus (Part 2)
  • What ChatGPT Needs Is Context
  • Comparing Cloud Hosting vs. Self Hosting
  • Operator Overloading in Java

Trending

  • Microservices With Apache Camel and Quarkus (Part 2)
  • What ChatGPT Needs Is Context
  • Comparing Cloud Hosting vs. Self Hosting
  • Operator Overloading in Java
  1. DZone
  2. Coding
  3. Frameworks
  4. Knockout JS, Attr Binding and Radio Button Values

Knockout JS, Attr Binding and Radio Button Values

Derik Whittaker user avatar by
Derik Whittaker
·
Jul. 18, 13 · Interview
Like (0)
Save
Tweet
Share
4.93K Views

Join the DZone community and get the full member experience.

Join For Free

When working with Knockout you have the ability to seamlessly bind to a radio button via the checked binder.  When working with Radio buttons it is very common to use the value attribute in order to know which radio button was selected.  In fact the code below should be very common if using this binding.

image

However, when I look at the code above I have 1 issue with it and that is the use of the hard coded value “1” as the value.  I HATE magic strings/values.  Having a raw value like this in your code does not lead to discoverability, I mean what what does ”1” mean?  I would much rather use a constant/enumeration here in order to provide better discoverability.  However, your straight up HTML does not lend itself to this.  For example the code below would NOT yield the results you are expecting.

image

However, we can harness the power of Knockouts Attr binding to solve our readability problem.  Take a look at the code below

image

By using the Attr binding I can use the Knockout binding engine to add the value 1 to my HTML at runtime which gives me the correct value as well having my enum value for readability at code time.  Below is the rendered HTML

image

Till next time,

P.S. If you want to learn more about Knockout watch my Pluralsight Course on Knockout JS

P.S.S. If you want to learn how to transition your XAML skills to HTML you can watch my course on Html for the XAML Developer

Knockout (web framework) Binding (linguistics)

Published at DZone with permission of Derik Whittaker, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Microservices With Apache Camel and Quarkus (Part 2)
  • What ChatGPT Needs Is Context
  • Comparing Cloud Hosting vs. Self Hosting
  • Operator Overloading in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: