0

I want to add a div to td tag of a datatable. Here is my HTML code:

table = $("#workLocation-table").DataTable({
            data: mainArray,
            "columnDefs": [
                { className: "details-control" , "targets": [0]},
                {
                    "order": [[1, 'asc']]
                },
                {
                "targets": -1,
                "width": "10%",
                "data": null,
                "defaultContent": '<div class="edit-wrapper"><span class="icn"><i class="fa fa-pencil-square-o" aria-hidden="true" id="edit"></i><i class="fa fa-trash-o" aria-hidden="true" id="delete"></i></span></div>'
            }]
        });

I want to add a div into targets 2 and provide "over-length" class to that div.

1
  • 2
    What kind of html code is this ? Commented Oct 24, 2017 at 6:13

2 Answers 2

1
table =  $("#workLocation-table").DataTable({
        data: mainArray,
        "columnDefs": [
            { className: "details-control" , "targets": [0]},
            {
                "order": [[1, 'asc']]
            },
            {
                "targets": 2,
                render : function(data, type, row) {
                    return '<div class="over-length">'+data+'</div>'
                } 
            },
            {
                "targets": -1,
                "width": "10%",
                "data": null,
                "defaultContent": '<div class="edit-wrapper"><span class="icn"><i class="fa fa-pencil-square-o" aria-hidden="true" id="edit"></i><i class="fa fa-trash-o" aria-hidden="true" id="delete"></i></span></div>'
        }]
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

var target = document.getElementById("id_of_the_element_inside_which_you_want_to_insert_div");
var d = document.createElement("div"); // Create div dynamically
d.setAttribute("class","over-length"); // Add "over-length" class to the dynamically created div
target.appendChild(d); // Now insert div inside target element

3 Comments

Why don't you use a render function ?
@PhilMaGeo thank you so much.I solved my issue using render function
:-) I use it every day :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.