Byron Sommardahl Byron Sommardahl - 1 year ago 92
HTML Question

Is there a way to set the page title by data-binding using Knockout.js?

I have a viewModel with a

property. I'd like to set the page title using that property. Here's what I tried already, which didn't work:

<title data-bind="text: Title"></title>
<span data-bind="text: Title"/> <!-- this displays the title properly -->

The browser title is blank/default instead of the value of my

Answer Source

Try giving your html element an id

<html id="htmlTop" xmlns="">

and applying your viewModel to it

ko.applyBindings(viewModel, document.getElementById("htmlTop"));


This works for me; I just ran this page and the title said "Hello". Double check your code for typos.

<html id="htmlTop">

      <title data-bind="text: title"></title>

      <script type='text/javascript' src='jquery.min.js'></script>
      <script type='text/javascript' src='knockout-1.2.1.js'></script>

      <script type="text/javascript">

          $(function () {
              var viewModel = { title: "Hello" };
              ko.applyBindings(viewModel, document.getElementById("htmlTop"));





enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download