简体   繁体   中英

Getting .closest() div id

I am trying to implement the code given in this answer . I'd like to simplify it even further by using closest() to find the id of the nearest div and using that as the location, but it doesn't work - I just get 'undefined' in my text box.

Here's the script:

function updateDetails(date) {
    var loc = $(this).closest("div").attr("id");

    $('#buy-form').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});    

    document.getElementById('date').value= date; 
    document.getElementById('venue').value= loc;   

    $('#resicheck').attr('disabled', true);
}

Here's the (simplified) markup:

<div id="Philadelphia">
    <a href="javascript:updateDetails('20-24 January');">20-24 January</a>
</div>

What am I doing wrong?

in HTML

you nedd to pass this

<div id="Philadelphia">
 <a href="javascript:updateDetails('20-24 January',this);">20-24 January</a>
</div>

IN JS

function updateDetails(date,this) {
               var loc = $(this).closest("div").attr("id");
               $('#buy-form').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});    
               document.getElementById('date').value= date; 
               document.getElementById('venue').value= loc;   
               $('#resicheck').attr('disabled', true);
            }

and by jquery you do like below very simple manner

Now in html

<div id="Philadelphia">
    <a href="#">20-24 January</a>
</div>

In JS

$("#Philadelphia").on("click","a",function(e){
 e.preventDefault();
var date=$(this).text();
   var loc = $(this).closest("div").attr("id");
                   $('#buy-form').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});    
                   $('#date').val(date); 
                   $("#venue").val(loc);   
                   $('#resicheck').attr('disabled', true);
});

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