Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative solutions to creating HTML content dynamically in Javascript

I am working on a web project. (Absolutely no previous experience). This project requires retrieving lot of content from the server dynamically(Like on the click of button). Now, the problem is, I have to display the content in some neat, formatted way. Like, collapsible lists, hyperlinks etc. I am currently using JavaScript for this purpose. Like this:

li = document.createElement("li");
li.innerHTML = "some_content";

I also need to add appropriate classes to those dynamically created elements in JavaScript as per the requirements of Bootstrap. But the code really looks very messy now. Are there any alternative solutions for avoiding all the dynamic creation of elements in JS, formatting etc.?

like image 577
user4444 Avatar asked Mar 30 '15 00:03

user4444


People also ask

How JavaScript can create dynamic HTML content?

JavaScript can be included in HTML pages, which creates the content of the page as dynamic. We can easily type the JavaScript code within the <head> or <body> tag of a HTML page. If we want to add the external source file of JavaScript, we can easily add using the <src> attribute.

Can HTML elements be create dynamically using JavaScript?

createElement() method you can create a specified HTML element dynamically in JavaScript. Once created, you can insert (or add) the element to your web page, or add it to a pre-defined element or a dynamically created element. In fact, you can create an entire form dynamically using this method.

Can we make dynamic website with JavaScript?

One of the many reasons JavaScript is so popular is because you can use it to make dynamic HTML. This is sometimes referred to as manipulating the DOM.


1 Answers

Seems like a perfect scenario for a knockoutJS solution. I am just posting a sample code here from their live example.

you just create the template like this -

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>

(notice the bindings in data-bind)

and create the view model and then apply it -

// Here's my data model
var ViewModel = function(first, last) {
    this.firstName = ko.observable(first);
    this.lastName = ko.observable(last);

    this.fullName = ko.pureComputed(function() {
        // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
        return this.firstName() + " " + this.lastName();
    }, this);
};

ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work

and you are good to go with this output, only a matter of minutes.enter image description here

I created a Survey Builder page with it, the most important part is knockout is fast and also supports all kinds of js libraries to work with like jQuery, AngularJS, etc. You will never need to worry about rendering ui, just get/set with data.

Try it out here - http://jsfiddle.net/rniemeyer/LkqTU/

like image 175
brainless coder Avatar answered Sep 27 '22 16:09

brainless coder