Create HTML elements using jquery

how can i create something like below div using jquery? preferably using something similar to $(document.createElement('div'));or something faster than$("<div spry:region="myDs">{hostname}</div>");

<div spry:region="myDs">
    {hostname}
</div>

Edited

How to put this code in another JS file and use it, coz "<%= request.getParameter(\"filepath\") %>"creates a problem

var cpath="<%= request.getParameter(\"filepath\") %>";
var myDs = new Spry.Data.XMLDataSet(cpath, "csmclient/system");     
$(document).ready(function(){
    $('<div>')                      // Creates the element
    .attr('spry:region','myDs') // Sets the attribute spry:region="myDs"
    .html('{hostname}')         // Sets the inner HTML to {hostname}
    .appendTo('body');
});
+3
source share
4 answers

I am surprised that no one has yet fully utilized the jQuery chain features:

$('<div>')                      // Creates the element
    .attr('spry:region','myDs') // Sets the attribute spry:region="myDs"
    .html('{hostname}')         // Sets the inner HTML to {hostname}
    .appendTo('body')           // Append the newly created element to body

Here's an example jFiddle that shows it in action.

UPDATE
Request javascript, script . , ASP.NET script, javascript, :

  • ASP.NET. - , yourscriptname.js.ashx.
  • javascript- (, Response.Write() - ...
  • src <script> somefile.js somefile.js.asp?filepath=your/file.path.

, javascript , "filepath", . , your/file.path - , , .

+10

jQuery :

jQuery('<div/>', {
    "spry:region": "myDS",
    text: '{hostname}'
}).appendTo('body');

+3

, document.createElement('div') - javascript, jQuery. jQuery:

var main = document.getElementById('main');//where you want the div to be added
var div = document.createElement('div');
div.setAttribute("spry:region","myDs");
div.innerHTML = "{hostname}";
main.appendChild(div);

jQuery DOM, :).

+2

$( "html tags" ). appendTo ( " " )

$('<div spry:region="myDs">{hostname}</div>').appendTo("body")
$('<div spry:region="myDs">{hostname}</div>').appendTo("#content")

var div = $(document.createElement('div'));
div.html("{hostname}").attr('spry:region=', "myDs").appendTo("body")
+1

All Articles