Freid001 Posted October 23, 2014 Share Posted October 23, 2014 Hi, I have this html code: <div class="names dropdown"> <input type="text" class="text_form"> <ul class="dropdown-menu"></ul> </div> Some js adds the following after an ajax call: <li id='selected'><a href='#'>Option</a></li> in between the <ul></ul> tags. I then have this js that listens for id='selected' and when clicked I want it to set the value of input to the value select $(this) however it does not do this. It just says undefined. Im not sure why PLEASE HELP! $( document ).on( "click", "#selected", function() { console.log( $(this).parents() ); $(this).closest('input').val( $this.text() ); }); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/ Share on other sites More sharing options...
requinix Posted October 23, 2014 Share Posted October 23, 2014 You didn't define $this. Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494511 Share on other sites More sharing options...
Freid001 Posted October 23, 2014 Author Share Posted October 23, 2014 How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494520 Share on other sites More sharing options...
Freid001 Posted October 23, 2014 Author Share Posted October 23, 2014 oh I see Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494521 Share on other sites More sharing options...
Freid001 Posted October 23, 2014 Author Share Posted October 23, 2014 Thats a typo sorry. yeah it still does not work Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494522 Share on other sites More sharing options...
Freid001 Posted October 23, 2014 Author Share Posted October 23, 2014 (edited) It is this: $( document ).on( "click", "#selected", function() { console.log( $(this).parents() ); $(this).closest('input').val( $(this).text() ); }); Edited October 23, 2014 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494523 Share on other sites More sharing options...
Freid001 Posted October 23, 2014 Author Share Posted October 23, 2014 (edited) It just returns this: [prevObject: jQuery.fn.jQuery.init[1], context: li#selected, jquery: "1.9.1", constructor: function, init: function…]context: li#selectedlength: 0prevObject: jQuery.fn.jQuery.init[1]__proto__: Object[0] Edited October 23, 2014 by Freid001 Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494524 Share on other sites More sharing options...
Freid001 Posted October 23, 2014 Author Share Posted October 23, 2014 I've tried $(this).closest('div') too and still it says undefined. Im not sure if its something wrong with the DOM in the HTML? What do you think? Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494525 Share on other sites More sharing options...
requinix Posted October 24, 2014 Share Posted October 24, 2014 What is saying that what is undefined? In your original code $this was the only thing that was actually undefined. Speaking of, what's your current code? I lost track of your changes. Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1494644 Share on other sites More sharing options...
Alex_ Posted November 1, 2014 Share Posted November 1, 2014 The .closest() function only looks for parents, not "any nearest" element in the DOM, so to speak. One alternative is to do something like this since the container seems pretty static.. $(document).on('click', '#select', function() { var mainContainer = $(this).parents()[1]; //<div class="names dropdown"> -- Index 1, since static. var inputField = $(mainContainer).find('input')[0]; //Index 0, because it's right below the <div> tag. console.log(inputField); }); Obviously it isn't great practise tho, if you had more dynamic content or happened to add/change a field you'd have to change the indexes as well. But assuming that's a pretty static layout (aside from the dynamic <li> tags), it should work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/292018-js-help-with-parents-closest/#findComment-1495462 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.