naijacoder
naijacoder

Reputation: 464

how to output html with jquery without appending to element?

I would like to output the html code below without appending it to any element. is this possible.I need to do that because the jquery plugin i'm using needs to have the clean html below Any ideas . Thanks

 $(document).ready(function() {

  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: "{}",
    url:"page.aspx/getData",
    dataType: "json",
    success: function(data) {

      if (data.hasOwnProperty("d"))
        DoSomething(data.d);
      else
        DoSomething(data);
    }
 });



function DoSomething(msg) {
   var SComms = msg;
   alert(msg);
 }

and html

    <blockquote>
        <p>Ut eu consectetur nisi. Praesent facilisis diam nec sapien gravida non mattis justo imperdiet. Vestibulum nisl urna, euismod sit amet congue at, bibendum non risus.</p>
        <cite>&ndash; Quote Author (Quote #1)</cite>
    </blockquote>

Upvotes: 0

Views: 901

Answers (3)

molokoloco
molokoloco

Reputation: 4602

Having the same problem, it's seem that jQuery template is based on append... (See source) For example this don't work

$.template('navTpl', '<ul>{{html li}}</ul>');
$.template('navItemTpl', '<li>${titre}</li>');

var navItemTplValue = [
    {titre: 'photos'},
    {titre: 'films'},
    {titre: 'musiques'}
];
var navItemTplHtml = $.tmpl('navItemTpl', navItemTplValue);

var navTplValue = {li: navItemTplHtml}; // FAIL, even with navItemTplHtml.html()
var navTpl = $.tmpl('navTpl', navTplValue).appendTo('body');

Sample test here : http://jsfiddle.net/molokoloco/wtHeX/

Upvotes: 0

Mike
Mike

Reputation: 2587

At the very least, you have to append the output to body, html, or document to see it in a browser. Of course, you can output it as a Javascript-alert, but I strongly discourage the usage of alert();.

EDIT 1

You can add the output to a variable.

var globalVariable = '';
$(document).ready(function() {
   $.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   data: "{}",
   url:"page.aspx/getData",
   dataType: "json",
   success: function(data) {
      if (data.hasOwnProperty("d"))
         DoSomething(data.d);
      else
         DoSomething(data);
   }
});

function DoSomething(msg) {
   var SComms = msg;
   globalVariable = SComms;
}

If you do it like this, you can access the variable later on. It is important however that you declare it outside of the ready() method or else the value is lost because it goes out of scope.

Upvotes: 1

gor
gor

Reputation: 11658

It is not possible. Everything you see on web page is DOM element. If you want to display something, just append to appropriate element. Alternatively you could use loging to console.

Upvotes: 1

Related Questions