Sunny Rahevar
Sunny Rahevar

Reputation: 115

Not able to append HTML code in jquery

I am not able to append my HTML code using jQuery. This is my HTML code:

<div class="am-right-section">
    <div class="right-quote-section">
    <img class="customer-profile-img" src="http://45.56.70.30/mijpaal_am/pub/media/images/avtaaar.png" width="100px" height="100px"><div class="centered">
    <div id="loading" style="display:none">
    <img src="http://45.56.70.30/mijpaal_am/pub/media/am-loader/loader.gif">
    </div>
    </div>
<form class="form customerdetail" action="http://45.56.70.30/mijpaal_am/accountmanager/accountmanager/amsave" id="customerdetail" method="post" data-hasrequired="* Required Fields" novalidate="novalidate">
    <fieldset class="fieldset">

                <div class="control">
                    <input name="customer_land" id="customer_land" class="customer_land required-entry1" aria-required="true" type="text">
                </div>

                <button type="submit" title="Submit" class="action submit primary">
                    <span>Generate Request</span>
                </button>

    </form>
    </div>
    </div>
        <div class="tab-content">
            <div class="tab-pane active" id="contact_01">kndsa jkasf kajf kndsa jkasf kajf  kndsa jkasf kajf  kndsa jkasf kajf  kndsa jkasf kajf  kndsa jkasf kajf  </div>

        </div>

This is my jQuery:

<script>
 jQuery('.add-contact').click(function(e) {
        e.preventDefault();
        var id = jQuery(".nav-tabs").children().length; //think about it ;)
        jQuery(this).closest('li').before('<li><a href="#contact_'+id+'">New Tab</a><span>x</span></li>');         
        jQuery('.tab-content').append('<div class="tab-pane" id="contact_'+id+'"><?php echo $template;?></div>');
});
</script>

I have set my HTML in $template variable successfully.But append does not works.my html does not append into the tab-content class.

Please solve my query.Thanks in advance!!!!

Upvotes: 1

Views: 81

Answers (2)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

Tested with the below. It works. Must be a problem with the content of $template.

<?php
        $template =<<<JS
        <div class="quote-status"> <input name="quote-status-value" value="0" checked="" type="radio"> Get Email and set value<br> <input name="quote-status-value" value="1" type="radio"> Set value<br> </div>
JS;
        ?>      
        <script>
 jQuery(document).ready(function(){
    jQuery('.add-contact').click(function(e) {
        e.preventDefault();
        var id = jQuery(".nav-tabs").children().length; //think about it ;)
        jQuery(this).closest('li').before('<li><a href="#contact_'+id+'">New Tab</a><span>x</span></li>');   
        jQuery('.tab-content').append('<div class="tab-pane" id="contact_'+id+'"><?php echo $template;?></div>');
  });
});

</script>

Upvotes: 1

Andrik007
Andrik007

Reputation: 57

I think, your jQuery code is running before page load. Try this:

jQuery(document).ready(function(){
    jQuery('.add-contact').click(function(e) {
        e.preventDefault();
        var id = jQuery(".nav-tabs").children().length;
        jQuery(this).closest('li').before('<li><a href="#contact_'+id+'">New Tab</a><span>x</span></li>');         
        jQuery('.tab-content').append('<div class="tab-pane" id="contact_'+id+'"><?php echo $template;?></div>');
    });
});

Upvotes: 1

Related Questions