1

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!!!!

11
  • No element with 'tab-content' as class exists. Commented Mar 24, 2018 at 10:39
  • I have not post entire HTML.But tab-content class is exist. Commented Mar 24, 2018 at 10:40
  • 2
    @SunnyRahevar first try to append simple markup without the $template. To rule out any problem that may be caused by $template Commented Mar 24, 2018 at 10:48
  • 1
    show output of $template Commented Mar 24, 2018 at 10:55
  • 1
    id or $template are the issue. console.log them both Commented Mar 24, 2018 at 11:00

2 Answers 2

1

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>');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

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>

4 Comments

can i put php variable between <<<JS and JS?
Yes you can. Make sure the last JS; has no indentation.
can you please send me with example? how to put the variable between that?
Just a simple $variable. If it's an array then do {$variable[0]} or {$variable['key']}

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.