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 8043973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:04:53+00:00 2026-06-05T05:04:53+00:00

I have a block of html markup that I’d like to databind to a

  • 0

I have a block of html markup that I’d like to databind to a div using Knockout JS. This is simple enough with the knockout html binding.

What I’d like to do is conditionally bind child elements within that html markup, based on their class. These elements can appear anywhere within the html markup, as they are inline elements (like span, a tags, etc).

The purpose of this is to databind click events to specific words or phrases within this text block, so that I can call a function (in this case, a modal with a definition of the target word or phrase).

Does anyone know how to do this? From what I can see, I have two main avenues to explore:

  1. Use jQuery to grab the elements by class, and somehow databind to then after the fact. I don’t know if this will work for various
    reasons – not having the DOM ready, not being able to databind in
    that method, etc.

  2. Use knockout’s template binding, and add an afterRender template callback that parses the text block for the elements and databinds
    them accordingly.

  3. A combination of the two above. (Yeah, I know, that’s three avenues.)

Has anyone done anything like this before? I’d like to get your advice and feedback, if possible. Thank you!

  • 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-05T05:04:56+00:00Added an answer on June 5, 2026 at 5:04 am

    I don’t recommend this approach unless you are sure that the html you are binding to is safe. The html binding looks like this.

    ko.bindingHandlers['html'] = {
        'init': function() {
            // Prevent binding on the dynamically-injected HTML 
            // (as developers are unlikely to expect that, and it 
            // has security implications)
            return { 'controlsDescendantBindings': true };
        },
        'update': function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            ko.utils.setHtml(element, value);
        }
    };
    

    Looking at this you could write a html2 binding

    ko.bindingHandlers['html2'] = {
        'init': function() {
            return { 'controlsDescendantBindings': false };
        },
        'update': function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            ko.utils.setHtml(element, value);
        }
    };
    

    http://jsfiddle.net/madcapnmckay/LDtuF/1/

    Be careful with this, you want to make sure the html cannot be used for insecure purposes.

    Hope this helps.

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

Sidebar

Related Questions

I have HTML markup like this: <div id=blocks> <div class=block></div> <div class=block></div> <div class=block></div>
I have this html <div id=cont> <div id=videowrapper class=fitmein> <div id=vimeoposition style=display: block;> <div
I have this markup: @using (Html.BeginRouteForm(Default, new { controller = Home, action = Form
I want to have a setup like this: <div id=block> <div class=btn>2</div> <div class=btn>1235e</div>
I have a block of HTML code that contains a button <button class=someButton onclick=openPage('test')></button>
I have a block of code that serializes a type into a Html tag.
I have this tag into my html file: {{block:my_test_block}} {{news:my_test_block2}} I need to parse
I have following HTML: <div class=olympics style=display: block; position: absolute; left: 250px; top: px;>
I have a series of different blocks of HTML that i'd like to load
I have a server-generated html like: <ul> <li><!-- few nested elements that form a

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.