Score:-2

Jquery to move field above the table - changes during page refresh

in flag

I have the below jquery code to move a multiselect field and a button above the table.

jQuery(document).ready(function($){
  $('#views-form-resources-block-1 .form-item-ka-types').insertBefore($('#views-form-myblock-block-1 table'));
  $('#views-form-resources-block-1 #edit-actions--2').insertAfter($('#views-form-myblock-block-1 .form-item-ka-types'));
});

In this code I am moving the ka-types field above myblock view. Pictorial representation as below:

enter image description here

This code works fine when I load the page at first. But when I refresh the page, the multiselect comes down and shows messed up. again when I refresh the page it works fine. not sure what the problem is. Any help on how this can be fixed?

Kevin avatar
in flag
The Javascript is not written in a way that Drupal will attach it when the dom changes, so when a form rebuilds, this will be lost. It's probably easier to override this Views template to move a filter vs force it with jquery.
Kevin avatar
in flag
Otherwise review the Drupal docs on Drupal Behaviors.
sonfd avatar
in flag
I would also recommend doing this with yourjavas view's template. JavaScript changes like this will cause the page contents to shift which is not a nice UX. This could even negatively affect your SEO as a page's [Cumulative Layout Shift (CLS)](https://web.dev/cls/) score is a factor.
Hermann Schwarz avatar
cn flag
Could you precise please, what the first refreshing means? A form submit? So maybe the DOM looks a little different after submitting.
Hermann Schwarz avatar
cn flag
You can also check if web tools console shows any JS error, so your JS is never executed after submitting the form (first refresh)
Jiah avatar
in flag
I am able to achieve this using Drupal Behaviors.
Score:1
in flag

The below is my solution for this:

(function($, Drupal) {
  Drupal.behaviors.searchResults = {
    attach: function() {
      $('#views-form-resources-block-1 .form-item-ka-types')
        .insertBefore($('#views-form-myblock-block-1 table'));
      $('#views-form-resources-block-1 #edit-actions--2')
        .insertAfter($('#views-form-myblock-block-1 .form-item-ka-types'));
    }
  };
}(jQuery, Drupal));

@Kevin is correct. When the form rebuilds the DOM changes are lost. Better understanding of Drupal behaviors link has great explanation and examples of Drupal Behavior.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.