I am stuck in a seemingly simple problem.
I want to get the received HTML request from an ajax request and see if a particular element has a specific class and take the HTML code of another response element and use it to replace something, etc.
$(function() { $('.js-nav').on('click', function(e) { e.preventDefault(); var $this = $(this); var $url = $this.attr('href'); $.get($url.url, function(response) { var $response = $(response); var $body = $response.find('.js-wrapper').html(); var _isLight = $response.find('.js-wrapper').hasClass('body-light'); console.log($body); console.log(_isLight); History.pushState({ state:$url }, $title, $url); }); }); });
The console returns this:
undefined false
Now the .js-wrapper element is not in the header, but in the div that is inside the body.
The HTML response (console.logged after receiving it with $ .get) looks like this:
<!doctype html> <html class="no-js" lang="en"> <head> <meta charset="utf-8"> <title> some title ... </title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div class="js-wrapper wrapper body-light"> <header class="js-header header"> <nav class="header-nav"> <ul class="js-header-nav-list header-nav-list"> <li class="header-nav-item"> </li> </ul> </nav> </header> <div class="content"> </div> <footer class="footer"> <section class="footer-newsletter"> </section> </footer> </div> </body> </html>
(I included the call to historyJS just in case, if he could do something with it)
Help, as always, is evaluated.
source share