简体   繁体   中英

Find Value of textarea from closest row of table

I have a button Update in One td I want to fetch value of both textarea of td from same tr. There are lots of tr so I have to use closest() method to find element. I have tried to find element but it's not working.

HTML :-

<tr>
    <td>
        <div class="agenda-date">
            <div class="dayofmonth color_font_date">2017-05-31</div>
            <div class="dayofweek">
                Wednesday                                            
            </div>
        </div>
    </td>
    <td>
        <div class="row margin_two_planner">
            <button type="button" class="btn btn-sm btn-primary">AM </button> 
            <a data-toggle="modal" href="#add_apt_status" class=""><input type="number" value="3" name="" class="number_planner"></a>
            <label class="control-label"> 6 </label>
        </div>
        <div class="row margin_two_planner">
            <button type="button" class="btn btn-sm btn-primary">PM </button> 
            <a data-toggle="modal" href="#add_apt_status" class=""><input type="number" value="4" name="" class="number_planner"></a>
            <label class="control-label"> 2 </label>
        </div>
    </td>
    <td style="width: 20%;"> 
            <textarea id="" class="form-control initial_cap appointment_note" required="required" rows="3" name="appointment_note" cols="50" aria-required="true" disabled=""> APT -1 </textarea>
    </td>
    <td style="width: 20%;"> 
        <textarea id="" class="form-control initial_cap holiday_note" required="required" rows="3" name="holiday_note" cols="50" aria-required="true" disabled=""> Holiday - 1 </textarea>
    </td>
    <td>
        <div class="text-right">
            <button type="button" id="" class="btn btn-primary appointment_edit_button">Edit </button>
            <button type="button" id="" onclick="planner_note_edit(1)" class="btn btn-md btn-primary appointment_update_button" style="display:none"> Update </button>
            <button type="button" id="" class="btn btn-md btn-cancel appointment_cancel_button" style="display:none">Cancel </button>
        </div>
    </td>
</tr>

Jquery :-

function planner_note_edit(planner_note_id)
{

        appointment_note = $(this).closest('td').find(".holiday_note").val();
        alert(appointment_note);
}

But I'm not able to fetch value of textarea from closest td.

You should get the textarea from current row .

So, please use .closest('tr')

appointment_note = $(this).closest('tr').find(".holiday_note").val();

Also, you have to pass the clicked button object. this in your function is a reference to the window object.

function planner_note_edit(planner_note_id,event)
{

    appointment_note = $(event.target).closest('td').find(".holiday_note").val();
    alert(appointment_note);
}

HTML

<button type="button" id="" onclick="planner_note_edit(1,event)" class="btn btn-md btn-primary appointment_update_button" style="display:none"> Update </button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM