Home > front end >  get all td value of same row by jquery
get all td value of same row by jquery

Time:04-12

                {% set i = 0 %}
                {% if user_papers|length > 0 %}
                    {% for user_paper in user_papers %}
                        {% set u_user = user_paper.user %}
                        {% set i = i 1 %}
                        {% set img = u_user.getUserImg() %}

                        {% set relationship = user.getRelationshipWithTarget(u_user.id) %}
                        {% set userAnswer = user_paper.getCompleteAnswer() %}
                        {% set userAnswerCount = userAnswer|length %}

                        <tr>
                            <td>{{ i }}</td>
                            <td>
                                <a href="{{ img }}" data-lightbox="roadtrip"><img src="{{ img }}" alt="User-Image" style="border-radius: 50%;height: 50px;width: 50px"></a><br>
                                {{ u_user.name }}
                                {% if relationship != false %}
                                    <br><span id=""  style="font-size: 12px;color: white">{{ t(relationship)}}</span>
                                {% endif %}
                            </td>
                            <td>{{ userAnswerCount }}</td>
                            <td>
                                <select id=""  data-rating="{{ user_paper.teacher_rating }}">
                                    <option value="0"></option>
                                    <option value="1"></option>
                                    <option value="2"></option>
                                    <option value="3"></option>
                                    <option value="4"></option>
                                    <option value="5"></option>
                                </select>
                                <span >{{ t('rating') }}: {{ user_paper.teacher_rating }}</span>
                            </td>
                            <td>
                                <p>
                                    <textarea id="w3review" name="w3review" rows="4" cols="50">
                                        {{ user_paper.teacher_feedback }}
                                    </textarea>
                                </p>
                            </td>
                            <td>
                                <a  href="/{{ module_name }}/{{ controller_name }}/result/{{ user_paper.ref_id }}" target="_blank">{{ t('result')}}</a>
                                <div >{{ t('save') }}</div>

                            </td>
                        </tr>
                    {% endfor %}
                {% endif %}

Okay above is the code , so it will generate multiple row (depending on the database). What i wanted to do is when user click on submit_paper button , able to retrieve {{ user_paper.ref_id }} , {{ user_paper.teacher_feedback }}

            $(document).on('click', '.submit_paper', function () {
            var paper_ref = $(this).closest('td');
                            console.log(paper_ref);

i tried with this code , it always show 'undefined'

CodePudding user response:

The code you provided should not give you undefined, it should give you a table cell element. I'm not sure why that would happen but the cod ebelow should give you the data you require.

$(document).on('click', '.submit_paper', function () {
    // The anchor just before has the ref_id in the url
    var ref_url = this.previousElementSibling.href;        
    var ref_id = ref_url.split('/').pop();
    console.log(ref_id);
    // The teacher feedback is in the only textarea in the row(which id should be unique btw)
    var teacher_feedback = $(this).closest('tr').find('textarea').val();
    console.log(teacher_feedback);
}
  • Related