javascript/ jquery

I'm using jQuery 1.12.4 to set get the value of the closest preceding element using class selector. I'm unable to select the closest element.

 $(function() { $("[class='quickSelect']").blur(function() { var obj = $(this); alert($(this).parent()); // alert($(this).closest("[class~='endDateField']")); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td align="right"> Start Date </td> <td> <input type="text" name="debitStartDate" value="" class="dateField startDateField"> </td> <td align="right"> End Date </td> <td> <input type="text" name="debitEndDate" value="" class="dateField endDateField"> </td> <td class="debitApportioner" style="align:right"> Quick Select </td> <td class="debitApportioner" colspan="2"> <select class="quickSelect"> <option> SELECT </option> <option> JANUARY </option> <option> FEBRUARY </option> <option> MARCH </option> </select> <input class="quickSelect" type="text" /> </td> </tr> </table> 

Try this:

$(".quickSelect").blur(function() {
    var obj = $(this);
    obj.closest('tr').find('.endDateField');  // will find the endDateField in the current row
  });

closest() only traverses up the trees ancestors and stops at the first element that matches the selector so the above says find the closest ancestor tr to the input then find any endDateField inside that tr

暂无
暂无

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.

Related Question jQuery closest class selector jQuery closest selector is not working and scroll & focus to a class jquery closest selector in text next,closest selector is not working Jquery jQuery remove class on closest JQuery Closest Class Select jQuery retrieve closest-to-unique selector jQuery data attribute selector for closest parent on windows scroll add class on closest selector jQuery Traversal Closest <div> of a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM