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

  • SEARCH
  • Home
  • 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 8431909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:58:56+00:00 2026-06-10T05:58:56+00:00

This is how i want to construct my html <a href=#>John <i class=person></i></a> And

  • 0

This is how i want to construct my html

<a href="#">John <i class="person"></i></a>

And with knockout.js this is what i have done.

<a data-bind="text:name"><i class="person"></i></a>

As you can guess whole elements(not just text) of anchor is removed becuse of text binding in this case whole tags inside of anchor is removed. My solution is at below.

<a data-bind="html: name() + '<i class="person"></i>'"></a>

string concat with html in data-bind is a solution but it has 2 big downside. ‘name’ propery is not safe so we can got html injection. Sedondly writing html inside of data-bind attributes is sucks.

Another solution is.

<a href="#"><span data-bind="text:name"></span><i class="person"></i></a>

I know we introducing new html markup for just solution. It is what i have found best.

What is the well known solution for this problem in knockout.js ?

Can we specify to just update text not whole elements inside of it to text binding via parameters ?

Or better solution ?

  • 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-10T05:58:57+00:00Added an answer on June 10, 2026 at 5:58 am

    Using the span is the preferred solution. If the text binding doesn’t replace all of the content, then it is hard for it to know what to update and not update the next time that it changes. If you want to always deal with the first child node of the element, then you could write a small custom binding to help.

    Here is a simple prependText binding. This would always replace the first child node of the element that contains the binding. So, you would want to ensure that the first node was at least a space.

    ko.bindingHandlers.prependText = {
        update: function(element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            //replace the first child
            element.replaceChild(document.createTextNode(value), element.firstChild);
        }        
    };
    

    Use it like:

    <a href="#" data-bind="prependText: name"> <span> another element</span></a>
    

    Sample: http://jsfiddle.net/rniemeyer/5CfzH/

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

Sidebar

Related Questions

I have a seemingly simple html construct: <div class=featured-image img-wrapper full-width> <a href=http://localhost/wordpress/?p=26> <img
I want to use the DbProvider class to construct a generic DAL component. This
I have the following construct in my HTML document: <div class=content> <section class=column></section> <aside
I have this piece of html in my page. <p><a href=# id=addQueryPart >Add</a></p> <div
I have this piece of html in my page. <p><a href=# id=addQueryPart >Add</a></p> <div
i want to realize a construct in MS SQL that would look like this
I want this text all on one line, not spaced on two sepearate lines.
I have a simple problem: I want to construct a regex that matches a
So I have a PHP stack that looks roughly like this Class Stack {
I have some data in the html format. I am using Html.fromHtml(String) when setting

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.