jQuery script doesn't work on Symfony4 Twig












0















Hope you're doing fine :)



I am not very used to javascript/jQuery, I am trying to do Symfony CollectionType, where I can click the button to add or remove TextType boxes.



enter image description here



This is an example, so if I click "x" it will remove that and if I click "Add Another Time", it will add one more TextType box.



The issue is, these buttons do not work, I've tried multiple codes already, I would love to make this one to work.



My Twig template:



{% extends 'base.html.twig' %}
{% block body %}
<div class="container">

{% form_theme form 'bootstrap_4_layout.html.twig' %}
{{ form_start(form) }}

<br>

Name {{ form_widget(form.name) }}
Price {{ form_widget(form.price) }}
Available {{ form_widget(form.available) }}
Date {{ form_widget(form.date) }}
<div class="row js-ticket-time-wrapper"
data-prototype="{{ form_widget(form.ticketTimes.vars.prototype)|e('html_attr') }}"
data-index="{{ form.ticketTimes|length }}">
{% for t in form.ticketTimes %}
<div class="col-xs-4 js-ticket-time-item">
<a href="#" class="js-remove-time pull-right">
<span class="fa fa-times"></span>
</a>
{{ form_row(t) }}
{{ form_row(t.name) }}
</div>
{% endfor %}
<a href="#" class="js-ticket-time-add">
<span class="fa fa-plus-circle"></span>
Add Another time
</a>
</div>
<button type="submit" class="btn btn-primary" formnovalidate>Save</button>
{{ form_end(form) }}


{% endblock %}


And this is jQuery, that I am using right now:



{{ parent() }}
<script>
jQuery(document).ready(function() {
var $wrapper = $('.js-ticket-time-wrapper');
$wrapper.on('click', '.js-remove-time', function(e) {
e.preventDefault();
$(this).closest('.js-ticket-time-item')
.fadeOut()
.remove();
});
$wrapper.on('click', '.js-ticket-time-add', function(e) {
e.preventDefault();
// Get the data-prototype explained earlier
var prototype = $wrapper.data('prototype');
// get the new index
var index = $wrapper.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$wrapper.data('index', index + 1);
// Display the form in the page before the "new" link
$(this).before(newForm);
});
});
</script>


Base.html.twig CSS:



<link href="{{ asset('vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="{{ asset('css/4-col-portfolio.css') }}" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">


Base.html.twig Javascript:



<script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>


Custom CSS (4-col-portfolio) contains:



body {
padding-top: 54px;
}

@media (min-width: 992px) {
body {
padding-top: 56px;
}
}

.portfolio-item {
margin-bottom: 30px;
}

.pagination {
margin-bottom: 30px;
}









share|improve this question




















  • 1





    do you use any jquery cdn?

    – hoover_D
    Nov 20 '18 at 17:33











  • @crazy_B I am not using anything if I understand correctly, should I use? And if yes, where should I put it?

    – phame
    Nov 20 '18 at 17:34











  • Could you maybe show the base.html.twig? Especially the stylesheets and javascripts blocks?

    – dbrumann
    Nov 20 '18 at 18:14











  • @dbrumann No worries, I've edited my post and added these! :)

    – phame
    Nov 20 '18 at 18:22






  • 2





    Hi @ArnasDamasickis, are there any console errors? You should definitly try to debug the code in Chrome debugger which is great -> javascript.info/debugging-chrome Second option is to use console.log and check whether elements are available while script executes :)

    – Damian Dziaduch
    Nov 20 '18 at 19:06


















0















Hope you're doing fine :)



I am not very used to javascript/jQuery, I am trying to do Symfony CollectionType, where I can click the button to add or remove TextType boxes.



enter image description here



This is an example, so if I click "x" it will remove that and if I click "Add Another Time", it will add one more TextType box.



The issue is, these buttons do not work, I've tried multiple codes already, I would love to make this one to work.



My Twig template:



{% extends 'base.html.twig' %}
{% block body %}
<div class="container">

{% form_theme form 'bootstrap_4_layout.html.twig' %}
{{ form_start(form) }}

<br>

Name {{ form_widget(form.name) }}
Price {{ form_widget(form.price) }}
Available {{ form_widget(form.available) }}
Date {{ form_widget(form.date) }}
<div class="row js-ticket-time-wrapper"
data-prototype="{{ form_widget(form.ticketTimes.vars.prototype)|e('html_attr') }}"
data-index="{{ form.ticketTimes|length }}">
{% for t in form.ticketTimes %}
<div class="col-xs-4 js-ticket-time-item">
<a href="#" class="js-remove-time pull-right">
<span class="fa fa-times"></span>
</a>
{{ form_row(t) }}
{{ form_row(t.name) }}
</div>
{% endfor %}
<a href="#" class="js-ticket-time-add">
<span class="fa fa-plus-circle"></span>
Add Another time
</a>
</div>
<button type="submit" class="btn btn-primary" formnovalidate>Save</button>
{{ form_end(form) }}


{% endblock %}


And this is jQuery, that I am using right now:



{{ parent() }}
<script>
jQuery(document).ready(function() {
var $wrapper = $('.js-ticket-time-wrapper');
$wrapper.on('click', '.js-remove-time', function(e) {
e.preventDefault();
$(this).closest('.js-ticket-time-item')
.fadeOut()
.remove();
});
$wrapper.on('click', '.js-ticket-time-add', function(e) {
e.preventDefault();
// Get the data-prototype explained earlier
var prototype = $wrapper.data('prototype');
// get the new index
var index = $wrapper.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$wrapper.data('index', index + 1);
// Display the form in the page before the "new" link
$(this).before(newForm);
});
});
</script>


Base.html.twig CSS:



<link href="{{ asset('vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="{{ asset('css/4-col-portfolio.css') }}" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">


Base.html.twig Javascript:



<script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>


Custom CSS (4-col-portfolio) contains:



body {
padding-top: 54px;
}

@media (min-width: 992px) {
body {
padding-top: 56px;
}
}

.portfolio-item {
margin-bottom: 30px;
}

.pagination {
margin-bottom: 30px;
}









share|improve this question




















  • 1





    do you use any jquery cdn?

    – hoover_D
    Nov 20 '18 at 17:33











  • @crazy_B I am not using anything if I understand correctly, should I use? And if yes, where should I put it?

    – phame
    Nov 20 '18 at 17:34











  • Could you maybe show the base.html.twig? Especially the stylesheets and javascripts blocks?

    – dbrumann
    Nov 20 '18 at 18:14











  • @dbrumann No worries, I've edited my post and added these! :)

    – phame
    Nov 20 '18 at 18:22






  • 2





    Hi @ArnasDamasickis, are there any console errors? You should definitly try to debug the code in Chrome debugger which is great -> javascript.info/debugging-chrome Second option is to use console.log and check whether elements are available while script executes :)

    – Damian Dziaduch
    Nov 20 '18 at 19:06
















0












0








0








Hope you're doing fine :)



I am not very used to javascript/jQuery, I am trying to do Symfony CollectionType, where I can click the button to add or remove TextType boxes.



enter image description here



This is an example, so if I click "x" it will remove that and if I click "Add Another Time", it will add one more TextType box.



The issue is, these buttons do not work, I've tried multiple codes already, I would love to make this one to work.



My Twig template:



{% extends 'base.html.twig' %}
{% block body %}
<div class="container">

{% form_theme form 'bootstrap_4_layout.html.twig' %}
{{ form_start(form) }}

<br>

Name {{ form_widget(form.name) }}
Price {{ form_widget(form.price) }}
Available {{ form_widget(form.available) }}
Date {{ form_widget(form.date) }}
<div class="row js-ticket-time-wrapper"
data-prototype="{{ form_widget(form.ticketTimes.vars.prototype)|e('html_attr') }}"
data-index="{{ form.ticketTimes|length }}">
{% for t in form.ticketTimes %}
<div class="col-xs-4 js-ticket-time-item">
<a href="#" class="js-remove-time pull-right">
<span class="fa fa-times"></span>
</a>
{{ form_row(t) }}
{{ form_row(t.name) }}
</div>
{% endfor %}
<a href="#" class="js-ticket-time-add">
<span class="fa fa-plus-circle"></span>
Add Another time
</a>
</div>
<button type="submit" class="btn btn-primary" formnovalidate>Save</button>
{{ form_end(form) }}


{% endblock %}


And this is jQuery, that I am using right now:



{{ parent() }}
<script>
jQuery(document).ready(function() {
var $wrapper = $('.js-ticket-time-wrapper');
$wrapper.on('click', '.js-remove-time', function(e) {
e.preventDefault();
$(this).closest('.js-ticket-time-item')
.fadeOut()
.remove();
});
$wrapper.on('click', '.js-ticket-time-add', function(e) {
e.preventDefault();
// Get the data-prototype explained earlier
var prototype = $wrapper.data('prototype');
// get the new index
var index = $wrapper.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$wrapper.data('index', index + 1);
// Display the form in the page before the "new" link
$(this).before(newForm);
});
});
</script>


Base.html.twig CSS:



<link href="{{ asset('vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="{{ asset('css/4-col-portfolio.css') }}" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">


Base.html.twig Javascript:



<script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>


Custom CSS (4-col-portfolio) contains:



body {
padding-top: 54px;
}

@media (min-width: 992px) {
body {
padding-top: 56px;
}
}

.portfolio-item {
margin-bottom: 30px;
}

.pagination {
margin-bottom: 30px;
}









share|improve this question
















Hope you're doing fine :)



I am not very used to javascript/jQuery, I am trying to do Symfony CollectionType, where I can click the button to add or remove TextType boxes.



enter image description here



This is an example, so if I click "x" it will remove that and if I click "Add Another Time", it will add one more TextType box.



The issue is, these buttons do not work, I've tried multiple codes already, I would love to make this one to work.



My Twig template:



{% extends 'base.html.twig' %}
{% block body %}
<div class="container">

{% form_theme form 'bootstrap_4_layout.html.twig' %}
{{ form_start(form) }}

<br>

Name {{ form_widget(form.name) }}
Price {{ form_widget(form.price) }}
Available {{ form_widget(form.available) }}
Date {{ form_widget(form.date) }}
<div class="row js-ticket-time-wrapper"
data-prototype="{{ form_widget(form.ticketTimes.vars.prototype)|e('html_attr') }}"
data-index="{{ form.ticketTimes|length }}">
{% for t in form.ticketTimes %}
<div class="col-xs-4 js-ticket-time-item">
<a href="#" class="js-remove-time pull-right">
<span class="fa fa-times"></span>
</a>
{{ form_row(t) }}
{{ form_row(t.name) }}
</div>
{% endfor %}
<a href="#" class="js-ticket-time-add">
<span class="fa fa-plus-circle"></span>
Add Another time
</a>
</div>
<button type="submit" class="btn btn-primary" formnovalidate>Save</button>
{{ form_end(form) }}


{% endblock %}


And this is jQuery, that I am using right now:



{{ parent() }}
<script>
jQuery(document).ready(function() {
var $wrapper = $('.js-ticket-time-wrapper');
$wrapper.on('click', '.js-remove-time', function(e) {
e.preventDefault();
$(this).closest('.js-ticket-time-item')
.fadeOut()
.remove();
});
$wrapper.on('click', '.js-ticket-time-add', function(e) {
e.preventDefault();
// Get the data-prototype explained earlier
var prototype = $wrapper.data('prototype');
// get the new index
var index = $wrapper.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$wrapper.data('index', index + 1);
// Display the form in the page before the "new" link
$(this).before(newForm);
});
});
</script>


Base.html.twig CSS:



<link href="{{ asset('vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="{{ asset('css/4-col-portfolio.css') }}" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">


Base.html.twig Javascript:



<script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>


Custom CSS (4-col-portfolio) contains:



body {
padding-top: 54px;
}

@media (min-width: 992px) {
body {
padding-top: 56px;
}
}

.portfolio-item {
margin-bottom: 30px;
}

.pagination {
margin-bottom: 30px;
}






jquery symfony twig






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:21







phame

















asked Nov 20 '18 at 15:57









phamephame

406




406








  • 1





    do you use any jquery cdn?

    – hoover_D
    Nov 20 '18 at 17:33











  • @crazy_B I am not using anything if I understand correctly, should I use? And if yes, where should I put it?

    – phame
    Nov 20 '18 at 17:34











  • Could you maybe show the base.html.twig? Especially the stylesheets and javascripts blocks?

    – dbrumann
    Nov 20 '18 at 18:14











  • @dbrumann No worries, I've edited my post and added these! :)

    – phame
    Nov 20 '18 at 18:22






  • 2





    Hi @ArnasDamasickis, are there any console errors? You should definitly try to debug the code in Chrome debugger which is great -> javascript.info/debugging-chrome Second option is to use console.log and check whether elements are available while script executes :)

    – Damian Dziaduch
    Nov 20 '18 at 19:06
















  • 1





    do you use any jquery cdn?

    – hoover_D
    Nov 20 '18 at 17:33











  • @crazy_B I am not using anything if I understand correctly, should I use? And if yes, where should I put it?

    – phame
    Nov 20 '18 at 17:34











  • Could you maybe show the base.html.twig? Especially the stylesheets and javascripts blocks?

    – dbrumann
    Nov 20 '18 at 18:14











  • @dbrumann No worries, I've edited my post and added these! :)

    – phame
    Nov 20 '18 at 18:22






  • 2





    Hi @ArnasDamasickis, are there any console errors? You should definitly try to debug the code in Chrome debugger which is great -> javascript.info/debugging-chrome Second option is to use console.log and check whether elements are available while script executes :)

    – Damian Dziaduch
    Nov 20 '18 at 19:06










1




1





do you use any jquery cdn?

– hoover_D
Nov 20 '18 at 17:33





do you use any jquery cdn?

– hoover_D
Nov 20 '18 at 17:33













@crazy_B I am not using anything if I understand correctly, should I use? And if yes, where should I put it?

– phame
Nov 20 '18 at 17:34





@crazy_B I am not using anything if I understand correctly, should I use? And if yes, where should I put it?

– phame
Nov 20 '18 at 17:34













Could you maybe show the base.html.twig? Especially the stylesheets and javascripts blocks?

– dbrumann
Nov 20 '18 at 18:14





Could you maybe show the base.html.twig? Especially the stylesheets and javascripts blocks?

– dbrumann
Nov 20 '18 at 18:14













@dbrumann No worries, I've edited my post and added these! :)

– phame
Nov 20 '18 at 18:22





@dbrumann No worries, I've edited my post and added these! :)

– phame
Nov 20 '18 at 18:22




2




2





Hi @ArnasDamasickis, are there any console errors? You should definitly try to debug the code in Chrome debugger which is great -> javascript.info/debugging-chrome Second option is to use console.log and check whether elements are available while script executes :)

– Damian Dziaduch
Nov 20 '18 at 19:06







Hi @ArnasDamasickis, are there any console errors? You should definitly try to debug the code in Chrome debugger which is great -> javascript.info/debugging-chrome Second option is to use console.log and check whether elements are available while script executes :)

– Damian Dziaduch
Nov 20 '18 at 19:06














1 Answer
1






active

oldest

votes


















0














I debugged my code in Chrome debugger javascript.info/debugging-chrome and found out what error I was getting, so I simply fixed that and everything started to work! :)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396856%2fjquery-script-doesnt-work-on-symfony4-twig%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I debugged my code in Chrome debugger javascript.info/debugging-chrome and found out what error I was getting, so I simply fixed that and everything started to work! :)






    share|improve this answer




























      0














      I debugged my code in Chrome debugger javascript.info/debugging-chrome and found out what error I was getting, so I simply fixed that and everything started to work! :)






      share|improve this answer


























        0












        0








        0







        I debugged my code in Chrome debugger javascript.info/debugging-chrome and found out what error I was getting, so I simply fixed that and everything started to work! :)






        share|improve this answer













        I debugged my code in Chrome debugger javascript.info/debugging-chrome and found out what error I was getting, so I simply fixed that and everything started to work! :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 11:34









        phamephame

        406




        406
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396856%2fjquery-script-doesnt-work-on-symfony4-twig%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)