1

I have few repeating jQuery scripts that I combined into one for better performance, but I am still not happy and I am sure there is an easier way of doing this.

$(document).ready(function(){
$('div.wrap-around1 *').addClass('outer1');
$('div.outer1').wrapAll('<div class="frame-h" />');
$('div.wrap-around2 *').addClass('outer2');
$('div.outer2').wrapAll('<div class="frame-v" />');
$('div.wrap-around3 *').addClass('outer3');
$('div.outer3').wrapAll('<div class="frame-v" />');
$('div.wrap-around4 *').addClass('outer4');
$('div.outer4').wrapAll('<div class="frame-b" />');
});
1
  • 2
    * is slooooooooow, really slow selector. Are you sure * is really needed? Without seeing your HTML any answer here is just guesswork. Commented Jan 26, 2018 at 23:57

1 Answer 1

1

The Universal Selector * is slow, (not much in CSS but) when used in JS. So probably you can use some right selectors, classes or something (cannot say without any HTML sample or in-depth description).

But your code can be refactored to:

  • Extract the number out of your class
  • Map Numbers to h, v, b characters

const num2Frame = {1:"h", 2:"v", 3:"v", 4:"b"}; // map class num to frame char


jQuery(function ($) { // DOM ready and $ alias secured

  $("[class^='wrap-around'], [class*=' wrap-around']").each(function() {
  
       // extract number from className
       const num = (this.className.match(/wrap-around(\d+)/)||'')[1];
       if (!num) return;
       $(this).find("*")  //instead of * use a specific selector
           .wrapAll(`<div class="frame-${num2Frame[num]}" />`)
           .addClass('outer'+ num); 
  });

});
.outer1{color: green;}
.outer2{color: gold;}
.outer3{color: gold;}
.outer4{color: red;}

.frame-h{ border: 1px solid green; }
.frame-v{ border: 1px solid gold; }
.frame-b{ border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="asd wrap-around1 qwe sdfg"><p>1</p></div>

<div class="asd wrap-around2 qwe sdfg"><p>2</p></div>


A total improvement would be refactoring your selectors and HTML and use the right tools for the job:

const num2Frame = {1:"h", 2:"v", 3:"v", 4:"b"}; // map class num to frame char


jQuery(function ($) { // DOM ready and $ alias secured

  $("[data-wrap]").each(function() {
       const num = $(this).data("wrap");
       $(this).children()
           .wrapAll(`<div class="frame-${num2Frame[num]}" />`)
           .addClass('outer'+ num); 
  });

});
.outer1{color: green;}
.outer2{color: gold;}
.outer3{color: gold;}
.outer4{color: red;}

.frame-h{ border: 1px solid green; }
.frame-v{ border: 1px solid gold; }
.frame-b{ border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div data-wrap="1"><p>1</p></div>
<div data-wrap="2"><p>2</p></div>

Sign up to request clarification or add additional context in comments.

2 Comments

@RokoCBuljan here's the jsfiddle.net/3ryokxkt but for some reason the js does not work there, anyways, you can see the html and get the idea. The frame-h,frame-v and frame-b classes add background color with border around, all over background image, hence the need for the extra wrapping div to cover Title and Link together. with one pseudo class creating bg and border around it. I hope it is now much clearer.
@Radi Just like in my example above, same for your example - seems to work quite well: jsfiddle.net/RokoCB/3ryokxkt/1 ( just added the missing jQuery library and - well, the suggested code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.