Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert div class into JQuery?

Hi i am trying to write the bootstrap panel head and body in JQuery.

Here is my HTML code:

<div class="panel panel-default">
        <div class="panel-heading">
                <h3 class="panel-title" id="panel_title">Demo</h3>
        </div>
        <div class="panel-body">
                <div id=print_received_table></div>
        </div>
</div>

If it is one div element we can create like this document.createElement('div') but what about if it is more than one div.

This class <div class="panel panel-default"> i would like to make the parent node.

I am stuck here only. please suggest me how to do?

Any help will be appreciated. Thanks in advance.

like image 364
Awanti Avatar asked Sep 29 '15 10:09

Awanti


People also ask

How append a div in another div using jQuery?

First, select the div element which need to be copy into another div element. Select the target element where div element is copied. Use the appendTo() method to copy the element as its child.

What is addClass and removeClass in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

What is insertAfter in jQuery?

The insertAfter() method is an inbuilt method in jQuery that is used to insert some HTML content after a specified element. The HTML content will be inserted after each occurrence of the specified element. Syntax: $(content).insertAfter(target)

How do you append a class in JavaScript?

Using . add() method: This method is used to add a class name to the selected element. Syntax: element.


2 Answers

You can do it as below:

var element=$('<div class="panel panel-default">'
              +'<div class="panel-heading">'
              +'<h3 class="panel-title" id="panel_title">Demo</h3>'
              +'</div>'
              +'<div class="panel-body">'
              +'<div id="print_received_table"></div>'
              +'</div>'
              +'</div>'
            );

Now you can use the element and append it anywhere in the DOM as below:

$('yourselectorID').append(element);

Other option would be creating element one by one and then appending it according to its structure finally appending it to DOM as below:

var parent=$('<div/>', {
    class: 'panel panel-default',
});

var heading=$('<div/>', {
    class: 'panel-heading',
});

var h3=$('<h3/>', {
    class: 'panel-title',
    id:'panel_title',
    text:'Demo'
});

var body=$('<div/>', {
    class: 'panel-body',
});

var prTable=$('<div/>', {
    id: 'print_received_table',
});

heading.append(h3);
body.append(prTable);
var element=parent.append(heading).append(body);

Now you have your element which can be appended to DOM using same .append method mentioned previously. But as of my opinion this feels fishy and difficult to read, and this is just one more way to do it.


UPDATE

The method your are asking me to show is using pure javascript and you can use appendChild and className etc as below:

var parent = document.createElement("div");
parent.className="panel panel-default";

var heading = document.createElement("div");
heading.className="panel-heading";
var h3=document.createElement("h3");
h3.className="panel-title";
h3.setAttribute('id','panel_title');
var t = document.createTextNode("DEMO"); 
h3.appendChild(t);
heading.appendChild(h3);

var pbody=document.createElement('div');
pbody.className="panel-body";
var prTable=document.createElement('div');
prTable.setAttribute('id','print_received_table');
pbody.appendChild(prTable);

parent.appendChild(heading);
parent.appendChild(pbody);

Now you can use parent element to append it anywhere in DOM using appendChild

like image 133
Guruprasad J Rao Avatar answered Oct 02 '22 05:10

Guruprasad J Rao


Pass the full html string to the constructor like this:

var element = '<div class="panel panel-default"><div class="panel-heading"><h3 class="panel-title" id="panel_title">Demo</h3></div><div class="panel-body"><div id=print_received_table></div></div></div>';


$('#result').append(element);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"><?div>
like image 36
Wesley Smith Avatar answered Oct 02 '22 06:10

Wesley Smith