0

I have to append 2 div to new div:

<script type="text/javascript">
        $(function() {
            $( "#table-filter_wrapper" ).append($( "<div class='main_tbl_btm_info'></div>" ));
            $( "#table-filter_info" ).appendTo( $( ".main_tbl_btm_info" ));
            $( "#table-filter_paginate" ).appendTo( $( ".main_tbl_btm_info" ));
        });
</script>

The problem is that it can't find table-filter_wrapper at ready.
When I use alert before append then it works.
How can I get that table-filter_wrapper is loaded or not.

5
  • 1
    Do you know when #table-filter_wrapper is injected into the DOM? Commented Oct 29, 2013 at 9:17
  • @David table-filter_wrapper is loaded at the ready and it take time so when code run it could not find table-filter_wrapper if we give some delay like by alert or other then it works .So actual problem is find table-filter_wrapper is loaded or not after load execute the code. Commented Oct 29, 2013 at 9:46
  • How are you "loading" the #table-filter_wrapper? At some point, you must be using append or similar, so just place this code after the append has been made. Commented Oct 29, 2013 at 9:49
  • @David #table-filter_wrapper is created by datatable for pagination and it is loaded on ready and I am using append after init the datatable Commented Oct 29, 2013 at 9:51
  • possible duplicate of Create <div> and append <div> dynamically Commented Feb 3, 2015 at 6:12

4 Answers 4

3

you need to delete $ from your append like this:

<script type="text/javascript">
        $(function() {
            $( "#table-filter_wrapper" ).append( "<div class='main_tbl_btm_info'></div>");
            $( "#table-filter_info" ).appendTo(".main_tbl_btm_info" );
            $( "#table-filter_paginate" ).appendTo( ".main_tbl_btm_info");
        });
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

This doesn’t make sense... appendTo can take a jQuery array as argument, so this wouldn’t solve anything.
but the div is not array in op question.
@mamdouhalramadan yea, the script can’t find table-filter_wrapper at ready, which would have nothing to do with your solution.
@mamdouhalramadan I don't think that is the problem, $( "<div class='main_tbl_btm_info'></div>" ) creates a new dom element and it will be accepted by jQuery
yes from david fiddle I saw it's working and it does make sense, but it doesn't mean that this is right to be done with elements already declared or being passed from other sources.
|
2

Your code works if the elements are available in the DOM: http://jsfiddle.net/XY7Sf/

<div id="table-filter_wrapper"></div>
<div id="table-filter_info"></div>
<div id="table-filter_paginate"></div>

If they are not available in the DOM, you need to run this code when they are, maybe in an ajax callback somewhere?

Comments

1

Try to target a wrapper div first or table and the use .find() to get the class you want to appentTo like:

var a = $(".wrapper").find(".main_tbl_btm_info")
$( "#table-filter_paginate" ).appendTo(a);

Comments

1

You want something like this, right ?

Demo JSFIDDLE

JS:

$(function() {
            $( "#table-filter_wrapper" ).append($( "<div class='main_tbl_btm_info'>main_tbl_btm_info</div>" ));

    $( ".main_tbl_btm_info" ).append($( "<div id='table-filter_info'>table-filter_info</div>" ));

    $( ".main_tbl_btm_info" ).append($( "<div id='table-filter_paginate'>table-filter_paginate</div>" ));

});

Comments

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.