0

I am trying to create multiple div element in Jquery. I had tried below solutions.

create html with jquery
Creating a div element in jQuery

I need to create below html element in jquery

<div class="r-c-grid ">
    <div class="r-c-imgmask">
        <img src="http://example.com/images/path.jpg">
    </div>
    <div class="r-c-gradient"></div>
    <h3 class="r-c-grid-title"><a href="http://example.com/link.php">Post Title</a></h3>
</div>

I am able to do this by above solutions but it is more complicated.Can anyone suggest me is there any better way to do this in latest jquery.

Community
  • 1
  • 1
  • 1
    Show the code you already have so we can suggest ways to improve it, rather than having to start from scratch. – Anthony Grist Nov 11 '13 at 12:38
  • Just wrap the HTML code in `$( )`.. – Mr_Green Nov 11 '13 at 12:40
  • I presume that you need it dynamic, right? – Dropout Nov 11 '13 at 12:40
  • Yes in particular condition I want to append this multiple `r-c-grid` div in my container div. Yes I can achieve this by append as suggested by @Rituraj. But is there any way to do this create dom element and add class and append inner html. –  Nov 11 '13 at 12:44

4 Answers4

2
var  str='<div class="r-c-grid ">'
        +'<div class="r-c-imgmask">'
         +'   <img src="http://example.com/images/path.jpg">'
        +'</div>'
        +'<div class="r-c-gradient"></div>'
        +'<h3 class="r-c-grid-title"><a href="http://example.com/link.php">Post Title</a></h3>'
    +'</div>';

$(str).appendTo(yourselector);

or can try

$(yourselector).append(str);

SEE DEMO

reference append() and appendTo()

Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
  • why global variable? or you forgot using `var`? – Mr_Green Nov 11 '13 at 12:43
  • 1
    FWIW, out of all methods suggested (including mine) this appears to achieve the best performance. This led me to an interesting discovery, though: why is this method (adding lots of pieces of string together) almost twice as fast as adding one unbroken string? See the test here: http://jsperf.com/adding-html-as-string – Ryan Williams Nov 11 '13 at 14:24
1

You can add the whole block in one go as follows:

$('body').append('\
    <div class="r-c-grid "> \
        <div class="r-c-imgmask"> \
            <img src="http://example.com/images/path.jpg"> \
        </div> \
        <div class="r-c-gradient"></div> \
        <h3 class="r-c-grid-title"><a href="http://example.com/link.php">Post Title</a></h3> \
    </div> \
');

Note that the backslashes are needed to break a string like this into multiple lines without causing syntax errors. This effectively comments out the line breaks.

Ryan Williams
  • 695
  • 5
  • 11
  • 1
    Recently, I had done the same but it didn't work. Now I understood why.. because I wasn't counting the trailing spaces in each line after the code to add the escape character as you did here :D. if there is a space after the escape character then everything mess ups. – Mr_Green Nov 11 '13 at 12:50
0

Simply try this,

$('body').append('<div class="r-c-grid ">
    <div class="r-c-imgmask">
        <img src="http://example.com/images/path.jpg">
    </div>
    <div class="r-c-gradient"></div>
    <h3 class="r-c-grid-title"><a href="http://example.com/link.php">Post Title</a></h3>
</div>');

Or try,

$rci=$('<div class="r-c-imgmask" />')
         .append('<img src="http://example.com/images/path.jpg">');
$rcg=$('<div class="r-c-gradient" />');
$h3=$('<h3 class="r-c-grid-title" />')
         .append('<a href="http://example.com/link.php">Post Title</a>');
$rc=$('<div class="r-c-grid " />').append($rci, $rcg, $h3);
$('body').append($rc);
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

Do you mean something semi-dynamic like this?

function addDiv(parentId, img, link, text){
    var div = '<div class="r-c-grid "><div class="r-c-imgmask">'+
        '<img src="http://example.com/images/'+img+'">'+
        '</div><div class="r-c-gradient"></div>'+
        '<h3 class="r-c-grid-title">'+
        '<a href='+link+'>'+text+'</a></h3></div>';
    document.getElementById(parentId).innerHtml += div;
}
Dropout
  • 13,653
  • 10
  • 56
  • 109