[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I compress this jQuery code?

Posted on 2014-08-30
5
Medium Priority
?
399 Views
Last Modified: 2014-08-30
Hi all,

how do I compress this jquery code? There are a lot of similar items that redundandly repeat themselves.

jQuery("th:contains('Date')").html("Data");
    jQuery("li.previous strong:contains('Previous Match')").html("PARTITA PRECEDENTE");
    jQuery("li.previous strong:contains('Previous player')").html("GIOCATORE PRECEDENTE");
    jQuery("li.previous strong:contains('Previous staff')").html("PRECEDENTE");
    jQuery("li.previous strong:contains('Previous story')").html("NEWS PRECEDENTE");
    jQuery("li.next strong:contains('Next Match')").html("PARTITA SUCCESSIVA");
    jQuery("li.next strong:contains('Next player')").html("GIOCATORE SUCCESSIVO");
    jQuery("li.next strong:contains('Next staff')").html("SUCCESSIVO");
    jQuery("li.next strong:contains('Next story')").html("NEWS SUCCESSIVA");
    jQuery(".sharrre-container span:contains('Share')").html("CONDIVIDI");
    jQuery(".page-title h2:contains('Match Details')").html("INFORMAZIONI");
    jQuery(".page-title h2:contains('Player Profile')").html("PROFILO GIOCATORE");

Open in new window


thanks in advance
0
Comment
Question by:badwolfff
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40295005
Since every line is different then how do you expect to "compress" it?
0
 

Author Comment

by:badwolfff
ID: 40295008
could I not create an array or use case or something similar?

Apart for these two lines:
    jQuery("th:contains('Date')").html("Data");
    jQuery(".page-title h2:contains('Match Details')").html("INFORMAZIONI");
which we can ignore here as I pasted them by mistake

all the others can be categorized as:
    jQuery("li.previous strong:contains('Previous Match')").html("PARTITA PRECEDENTE");
    jQuery("li.previous strong:contains('Previous player')").html("GIOCATORE PRECEDENTE");
    jQuery("li.previous strong:contains('Previous staff')").html("PRECEDENTE");
    jQuery("li.previous strong:contains('Previous story')").html("NEWS PRECEDENTE");


    jQuery("li.next strong:contains('Next Match')").html("PARTITA SUCCESSIVA");
    jQuery("li.next strong:contains('Next player')").html("GIOCATORE SUCCESSIVO");
    jQuery("li.next strong:contains('Next staff')").html("SUCCESSIVO");
    jQuery("li.next strong:contains('Next story')").html("NEWS SUCCESSIVA");


    jQuery(".page-title h2:contains('Match Details')").html("INFORMAZIONI");
    jQuery(".page-title h2:contains('Player Profile')").html("PROFILO GIOCATORE");


Perhaps, for example in the first case, create an array (Previous Match, Previous player, Previous staff, Previous story) and match them with another array contains, in the same index positions, (PARTITA PRECEDENTE, GIOCATORE PRECEDENTE, PRECEDENTE, NEWS PRECEDENTE) and then for each li.previous strong:contains could I not match [id of this] from array 1 with the corresponding id in array 2?

I ask this as I have quite a few more sets like this and I don't want to have to keep repeating useless lines.

thanks
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40295013
For the sake of a few characters you wouldn't gain anything but for example.

myArray=['Previous Match',"PARTITA PRECEDENTE",'Previous player',"GIOCATORE PRECEDENTE"]

var arrayLength = myArray.length;
for (var i = 0; i < arrayLength; i=i+2) {
    jQuery("li.previous strong:contains('"+myArray[i]+"')").html(myArray[i+1]);
}

Open in new window

0
 

Author Comment

by:badwolfff
ID: 40295015
Thanks, that is great.
Could this be done using jQuery each function?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40295020
A foreach would serve no purpose in this case
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question