jQuery - How to get an element's class from ajax request

Marine Le Borgne

I'm loading my website's pages with ajax by replacing the content inside the main tag. Problem is, using Wordpress, each page has its own body classes that are useful for styling purposes, so I want to replace the old page's body classes by the next page's classes.

I thought i'd run a new ajax request to get the whole html page, then check for the body element, then use .attr("class") to get the list of class and finally replace the old body classes by the new one...

But the classes always return undefined instead of a list of classes.

EDIT: I tried to use .cd-main-content instead of body and weirdly it works, I get the classes of this element. So I assume now that the problem doesn't come from my syntax but from the element itself.
How can I possibly get it to work on the body element ? (I already tried to replace .find by .filter but it doesn't work either.)

HTML stucture

<body id="body" class="home page-id-number other-classes">
    <main>
        <div class="cd-main-content">
            <!-- inside is the dynamically loaded content-->
        </div>
    </main>
</body>

jQuery

$.ajax({url: url, 
    success: function(data){
        var body = $(data).find("#body"); 
        var classes = body.attr("class");
        console.log(data); //returns the html as expected
        console.log("body : "+body); //returns [object Object]
        console.log("classes : "+classes); //returns undefined
    } 
});
Igor Sorish

"body" tag filtering by jQuery when getting from string.

So $(data)[0] will back all content, without body.

Also use filter, not "find" So you can get classes like that:

$.ajax({url: url, 
    success: function(data){
       //replace body tag
       data = data.replace("<body", "<container").replace("body>", "container>");
       var classes = $(data).filter("container").attr("class");
        $("body").attr("class", classes);
    } 
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the response text from an AJAX request using jQuery

From Dev

how do i get a response from a jquery ajax request

From Dev

How to get class name dynamically from element on jQuery?

From Dev

how to get length of jQuery .load AJAX request

From Dev

Get element by class from selection of elements in jQuery

From Dev

How to detect if GET request comes from an AJAX request or browser request?

From Dev

How to send a token with an AJAX request from jQuery

From Dev

Jquery context not returning existing element from ajax request

From Dev

How to get the data response from ajax request?

From Dev

How to get the data response from ajax request?

From Dev

How to get a data from html element from ajax responseText without jquery

From Dev

How to get jQuery UI datepicker to work correctly in a form that's loaded via ajax request

From Dev

How to get jQuery UI datepicker to work correctly in a form that's loaded via ajax request

From Dev

How to get source element from AJAX event?

From Dev

how to get an element added from an ajax response

From Dev

How to replace text of element after AJAX request using Jquery

From Dev

How to Get Element From Path Not Class or Id

From Dev

How to get an element from the ForeignKey of a class?

From Dev

How to pass element from model in ajax post request?

From Dev

how to get value of specifice tag element from user defined class in jquery

From Dev

jquery: ajax get request not working

From Dev

Jquery .ajax get request with Odata

From Dev

JQuery get element id from ajax loaded data

From Dev

JQuery get element id data from ajax loaded

From Dev

How to get "data" from JQuery Ajax requests

From Dev

How to get responds from php to jquery ajax

From Dev

How to select a class's last element for each occurrence of the class in jQuery

From Dev

How to get information from a clicked element with JQuery?

From Dev

How to get element value from a form with jQuery

Related Related

  1. 1

    How to get the response text from an AJAX request using jQuery

  2. 2

    how do i get a response from a jquery ajax request

  3. 3

    How to get class name dynamically from element on jQuery?

  4. 4

    how to get length of jQuery .load AJAX request

  5. 5

    Get element by class from selection of elements in jQuery

  6. 6

    How to detect if GET request comes from an AJAX request or browser request?

  7. 7

    How to send a token with an AJAX request from jQuery

  8. 8

    Jquery context not returning existing element from ajax request

  9. 9

    How to get the data response from ajax request?

  10. 10

    How to get the data response from ajax request?

  11. 11

    How to get a data from html element from ajax responseText without jquery

  12. 12

    How to get jQuery UI datepicker to work correctly in a form that's loaded via ajax request

  13. 13

    How to get jQuery UI datepicker to work correctly in a form that's loaded via ajax request

  14. 14

    How to get source element from AJAX event?

  15. 15

    how to get an element added from an ajax response

  16. 16

    How to replace text of element after AJAX request using Jquery

  17. 17

    How to Get Element From Path Not Class or Id

  18. 18

    How to get an element from the ForeignKey of a class?

  19. 19

    How to pass element from model in ajax post request?

  20. 20

    how to get value of specifice tag element from user defined class in jquery

  21. 21

    jquery: ajax get request not working

  22. 22

    Jquery .ajax get request with Odata

  23. 23

    JQuery get element id from ajax loaded data

  24. 24

    JQuery get element id data from ajax loaded

  25. 25

    How to get "data" from JQuery Ajax requests

  26. 26

    How to get responds from php to jquery ajax

  27. 27

    How to select a class's last element for each occurrence of the class in jQuery

  28. 28

    How to get information from a clicked element with JQuery?

  29. 29

    How to get element value from a form with jQuery

HotTag

Archive