[Support request] Reorder div elements with jQuery

Home Forums Support Reorder div elements with jQuery

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #955826
    George

    I am trying to reorder two div elements in a template that get generated by a plugin. I insert

    <script>
    (function($) {
    	jQuery('.class2').insertBefore('.class1');
    }) ( jQuery );
    </script>

    through a hook element at wp_footer but it doesn’t seem to work. I have also tried it in wp_header. I don’t see any errors in the console, either. Is that the correct way of doing it?

    #955838
    David
    Staff
    Customer Support

    Hi there,

    ideally scripts go in the footer so they are executed after the elements are loaded. Can’t see an issue with the script – i tested this on a clean GP install to move the content above the nav:

    (function($) {
    	$('.container').insertBefore('.main-navigation');
    }) ( jQuery );

    So it may be a specificity issue you’re having ie. are those element classes unique.

    #955852
    George

    The divs exist in a modal that loads the whole content. Could that be an issue?

    #955855
    David
    Staff
    Customer Support

    Most probably – if the modal content is being called by ajax or another script then those elements won’t exist when the script is executed.

    #955970
    George

    Is there anything I can do to intercept the ajax click? I have modified my first post to include a URL. The modal will appear when you click on one of the portfolio items.

    #955977
    David
    Staff
    Customer Support

    I believe you would need to use an event handler to execute yours script – so when the element is clicked or the ajax modal is loaded. This stack overflow goes into some detail on that:

    https://stackoverflow.com/questions/16062899/jquery-doesnt-work-after-content-is-loaded-via-ajax

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.