Check element and replace - JS repeat

Asked

Viewed 81 times

0

var test = new Array();

test["102-1"] = new Array("102-1", "Elemento");

$(document).ready(function () {

    $("a[name='" + test["102-1"][0] + "']").attr({
        "data-original-title":test["102-1"][1]
    });
});

This code checks the page element (in this case it checks the attribute 'name' within the tag a) and if it is equal to the Array 'first', it adds the attribute 'data-original-title' with the information of the Array 'first' at position [1].

How can I improve this code to get smarter? Because this Array will have more than 209 items. For example:

test["102-1"] = new Array("102-1", "Elemento");
test["102-2"] = new Array("102-2", "Elemento");
test["102-3"] = new Array("102-3", "Elemento");
test["103-1"] = new Array("103-1", "Elemento");
... etc

And for that I will have to create many

$("a[name='" + test["102-1"][0] + "']").attr({
    "data-original-title":test["102-1"][1]
});

Is there any way to reduce, causing the JS code to check and repeat the steps to replace the right items with the right Array?

Hugs to all!

  • But you have all numbers written in full ? Type test["duzentos e nove"] ?

  • Yes! I want to see if there is any possibility of not repeating the replacement part, in case the $("a[name='" + test["primeiro"][0] + "']").attr({
 "data-original-title":test["primeiro"][1]
}); This way, in the future, I can change only the information in the Array instead of changing all the code and avoid leaving too much also kkk...

  • Leaving for example from this conversion to full numbers here at Sopt you can already turn your code into a for simple

  • In fact what I said will not even work because you have the number in ordinal, ie "first", "second" and so on, instead of "one", "two", etc.. It means you first have to get a code/library to do that conversion for you, and from that point it’s easy.

  • Actually it would work! I used 'first', 'second', 'third' as a test. The numbers are actually '102-1', '102-2', '102-3', '103-1' etc... I’m running new tests. Try applying for to assign values on the page

  • But what is the need to have the number spelled out then ? It seems to me that you are complicating. You cannot simply use the identifier you have ?

  • Sorry! I edited and put the real numbers...

  • What is the whole range of numbers that exist ? It is always number and numero-1, numero-2 and numero-3 ?

  • This does not have a 'pattern'. These numbers are from GRI. For example, the GRI 102 pattern ranges from -1 to -56, but is not sequential. 102-18 jumps to 102-40. I believe the difficulty is not in the numbers or is it? My last option is to do as in the example, write the hand to replace the existing items on the page.

  • No default has no way to automate, unless somehow you can tell which ones are on the JS scan page.

  • I think about doing it this way. First he checks the available ones and then he makes the change. But for now, everything is indicating that I will have to use this way. Repeat the code twice

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.