Passing HTML through JavaScript

Guilherme Oderdenge

The goal

Pass to the DOM strings with HTML and render it.

The scenario

I'm extending properties of an observable (with KnockoutJS) using the follow syntax:

self.showDetails.subscribe(function (context) {
    var details = this.showDetails();
    details.nameWithCnpj = context.name() + " <small>" + context.cnpj() + "</small>";
}, this);

If you pay attention on the following line, you can see the HTML on it:

details.nameWithCnpj = context.name() + " <small>" + context.cnpj() + "</small>";

When the <small></small> tag arrives on the HTML, it is rendered as string instead of vanilla HTML.

The container that houses the nameWithCnpj (using KnockoutJS) is the following:

<h2 class="bold float-left" data-bind="text: nameWithCnpj"></h2>

So I ask: How can I teach to JavaScript (or HTML) that the nameWithCnpj variable should be a DOM element instead of a simple string?

nemesv

You need to use the html binding instead of text:

The html binding causes the associated DOM element to display the HTML specified by your parameter.

Typically this is useful when values in your view model are actually strings of HTML markup that you want to render.

So change your view to:

<h2 class="bold float-left" data-bind="html: nameWithCnpj"></h2>

If you want to be more MVVM you can create a template which encapsulates your formatting logic and use the template binding:

<h2 class="bold float-left" 
    data-bind="template: { name: 'nameWithCnpj', data: showDetails}"></h2>

<script id="nameWithCnpj" type="text/html">
   <span data-bind="text: name"></span>
   <small data-bind="text: cpnj"></small>
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing a variable to javascript through HTML Element

Passing a JavaScript variable to the view through a HTML form

Passing a variable to a function through a button (HTML/Javascript)

Passing Variable through JavaScript from one html page to another page

Array Passing through javascript

Passing an object variable through html

Passing values through javascript to php

passing javascript options in HTML

Passing parameters in Javascript and html

Passing parameters into Javascript in HTML

Passing the JavaScript variable into HTML

Creating and Passing HTML in Javascript

In a Directive, passing function arguments through to the html template

Passing a HTML array to PHP through jQuery AJAX

Passing a variable value through a html button

Syntax issues with passing through html in an ejs function

querying javascript through html

Passing javascript/Jquery array to php through a form

javascript passing arguments through functions and atribution with '='

Passing axios response through JavaScript object

Passing JavaScript array to PHP through jQuery $.ajax

Passing a JavaScript Array To PHP Through JQuery $.post

Trouble passing arguments through an initialiser function in javascript

Issue passing PHP variables through Javascript and back

Passing variables through PHP -> Javascript -> AJAX - Options?

Passing argument through an onchange method in Javascript

variable is not passing from javascript to php through ajax

Passing an argument to a Javascript function through PHP

Javascript passing through formatted int as string to function

TOP Ranking

HotTag

Archive