0

I got this code and I want to put values in jQuery. Thats working. I put $('a[href*="671"]') but I want to swap 671 with values3[i]. There are three values and three rows. It should have added the golden class.

// divide string to 3 letters and comma, 3 letters and comm...
function formatNumber (num) {
      return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}


// output values from golden classes
var elements = document.getElementsByClassName("golden");
var values = '';
for(var i=0; i<elements.length; i++) {
    values += elements[i].innerText;
}
var values2 = formatNumber(values);
var values3 = values2.split(",");
document.write(values3); // output 671,673,676
document.write('<br>');


// output values from hrefo classes
var elements = document.getElementsByClassName("hrefo");
var hrefs = '';
for(var i=0; i<elements.length; i++) {
    hrefs += elements[i].href.slice(-3);
 
}

var hrefs2 = formatNumber(hrefs);
var hrefs3 = hrefs2.split(",");
document.write(hrefs3); //output 672,671,670,673,676,675,667,666,674 


// output of equals values from two strings
for( var i = 0; i < values3.length; i++){
    if( hrefs2.indexOf(values3[i] ) > -1 ){
       console.log( 'hrefs3 contains number ' + (values3[i]) );
       document.write('<br>');
       document.write(values3[i]); // output 671 673 676

       
       // bg color change 
  $('a[href*="671"]') 
  .closest( "tr" )
  .addClass('golden');

    }
}
<tr>

<td><a class="hrefo" href="?page=view&amp;id=672"> Warera STARTED</a></td>
<td class="hidden-480"> warera-global.com </td>
<td> 72(110) / 1000 </td>
<td class="hidden-480"> 100 % </td>
<td> 100 </td>
 <td> 8.6 </td>
<td> PVP </td>
<td class="hidden-480"> RL MAP </td>
<td><a href="?page=list&amp;country=Brazil"> Brazil</a> </td>
</tr><tr class="golden">

<td><a class="hrefo" href="?page=view&amp;id=671"> Evolera FunPVP</a></td>
<td class="hidden-480"> evolera.se </td>
<td> 58(293) / 500 </td>
<td class="hidden-480"> 100 % </td>
<td> 10 </td>
<td> 8.6 </td>
<td> PVP </td>
<td class="hidden-480"> RL MAP </td>
<td><a href="?page=list&amp;country=Sweden"> Sweden</a> </td>
</tr>

              

So only what i want is to check id=671 in tr>td>a>href and if it equals with values3[i] or $values3 (that output same values - see higher) then give class to tr

Szmycu
  • 33
  • 5
  • Your question is _vague_. Please post the related HTML and `values3` variable and be more specific. – undefined Nov 27 '15 at 17:13
  • Sure! Try to add the HTML you're working with. – wilsotobianco Nov 27 '15 at 17:17
  • 1
    You really shouldn't use `document.write` see [Why is document.write considered a “bad practice”?](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – Wesley Smith Nov 27 '15 at 17:19
  • values3[i] output three values 671 673 676. I want to jquery code in bottom of js code addClasses only to where href got these values – Szmycu Nov 27 '15 at 17:34
  • Im not using document.write I am only testing output values !! – Szmycu Nov 27 '15 at 17:46

2 Answers2

0
    $("a[href*='"+values3[i]+"']")
    .closest( "tr" )
    .addClass('golden');

That solved problem thanks for comments :)

Szmycu
  • 33
  • 5
-3

The issue is a typo - hrefs3 is mistyped as hrefs2 in line 3.

Or else the comment in line 4 is wrong.

Euan M
  • 1,048
  • 10
  • 20
  • 1
    How do you know that? We don't know how some of the variables are defined here. – undefined Nov 27 '15 at 17:15
  • THis is a comment not an answer – Wesley Smith Nov 27 '15 at 17:16
  • This should be a comment! – wilsotobianco Nov 27 '15 at 17:17
  • His code does not work. He asked why. The answer is "because of his typo" – Euan M Nov 27 '15 at 17:18
  • ur right! it has no matter here. I want only put values3[i] in place of 671 and then three rows should be with class golden – Szmycu Nov 27 '15 at 17:20
  • 1
    @EuanM I think the point is that the question isn't structured or stated well enough to even understand the problem. You've simply noticed an inconsistency between a variable name and text in the `console.log` and stated that this must be the error. This is purely supposition and is not appropriate as an **answer** to a question on SO. This would however be acceptable as a comment like **Your variable name is `hrefs2` but your `console.log` statement says hrefs3, are you sure one of those is not a typo?`**. – Wesley Smith Nov 27 '15 at 17:31
  • If that comment happens to solve the problem, GREAT the OP can then delete the question because a typo question will never help anyone else in the future of SO. This is a comment not an answer. – Wesley Smith Nov 27 '15 at 17:31
  • @DelightedD0D No - it is a good *answer* to an *unsuitable* question. And no, it was not *just* noticing an inconsistency. – Euan M Nov 27 '15 at 18:28
  • Surely the OP having posted their solution illustrates our point – Wesley Smith Nov 27 '15 at 18:57