0

I used the last answer from this question to Add Lines to my Poll which i am creating in Symfony but the string i am appending is a bit longer then the one in the answer

('<div><input type="text" name="mytext[]"/><a href="#" class="delete">Delete</a></div>')

Mine looks like that (with twig, u don't have to understand everything in there, important is only the line <div><a href="#" <class="delete">Delete</a></div>' at the bottom):

'<div class="ul-div"><ul class="lst-none">\n' +
'    <li>\n' +
'        {% for Field in lineArray %}\n' +
'            <div class="{% if loop.first %}lineFieldFirst{% else %}lineField{% endif %}">\n' +
'                {% if Field.getType() == "choice" %}\n' +
'                    <select name="field_choice_{{ Field.getId() }}[]" id="field_choice_{{ Field.getId() }}">\n' +
'                        {% for Fieldvalue in Field.getFieldvaluesBySO() %}\n' +
'                            <option value="{{ Fieldvalue.getValue() }}">{{ Fieldvalue.getTranslationName(app.request.getLocale()) }}</option>\n' +
'                        {% endfor %}\n' +
'                    </select>\n' +
'                {% elseif Field.getType() == "checkbox" %}\n' +
'                    <label for="field_checkbox_{{ Field.getId() }}">{{ Field.getTranslationName(app.request.getLocale()) }}</label>\n' +
'                    <input type="checkbox" id="field_checkbox_{{ Field.getId() }}" name="field_checkbox_{{ Field.getId() }}[]">\n' +
'                {% elseif Field.getType() == "date" %}\n' +
'                    <label for="field_date_{{ Field.getId() }}">{{ Field.getTranslationName(app.request.getLocale()) }}</label>\n' +
'                    <input type="text" class="datepicker" id="field_date_{{ Field.getId() }}" name="field_date_{{ Field.getId() }}[]">\n' +
'                {% elseif Field.getType() == "number" %}\n' +
'                    <input type="number" id="field_number_{{ Field.getId() }}" name="field_number_{{ Field.getId() }}[]" min="0" value="0" required>\n' +
'                {% elseif Field.getType() == "text" %}\n' +
'                    <label for="field_text_{{ Field.getId() }}">{{ Field.getTranslationName(app.request.getLocale()) }}</label>\n' +
'                    <input type="text" id="field_text_{{ Field.getId() }}" name="field_text_{{ Field.getId() }}[]">\n' +
'                {% endif %}\n' +
'            </div>\n' +
'        {% endfor %}\n' +
'        <div><a href="#" <class="delete">Delete</a></div>' +
'    </li>\n' +
'</ul></div>'

Clearly the function

$(wrapper).on("click", ".delete", function(e) {
    e.preventDefault();
    $(this).parent('div').remove();
    x--;
})

does not work anymore because it only deletes the div around the <a> tags, but even when i:

  • remove the div around that
  • use parent('div.ul-div')
  • use parent('ul')
  • use parent('div').parent('div')

The Button doesn't delete the whole Line! What am i doing wrong here?

lxg95
  • 553
  • 2
  • 8
  • 28

1 Answers1

0

I don't know about Twig so, unless it's part of Twig, you seems to have a typo in your <div><a href="#" <class="delete">Delete</a></div> before the word class there is a < sign you should remove.

Also, Try using closest() instead of parent() since the last one only travels a single level up. closest() will get the first element matched no matter how many levels up, so you could even remove the <div class="ul-div"> with it.