user262430 user262430 - 7 months ago 25
Javascript Question

String comparison of variable not working

Does anyone have a clue what could be wrong with my JS. I have stared at this for a day and cannot come up with a fix. Any pointers would be great. I am not looking for comments on the code. Solutions only please.

var obj
returns
0of0
and
11of11
.

JS

$('tbody tr td .h5').parents('tr').each(function(){

var obj = $(this).find('span.meter-description').html().replace(/ /g, "");

console.log(obj);
console.log('--');

if(obj == '0of0'){
alert('hello');
}

});


HTML

<tr class="evenLocation separator-bttm">
<td class="text-middle"><strong class="h5 text-primary">35061077(<abbr title="Mail Out">M/O</abbr>)</strong></td>
<td class="text-center"><div class="meter blue"><span style="width: 0%"></span></div><span class="meter-description">0 of 0</span></td>
<td class="text-center">0</td><td class="text-center">0.0</td>
<td>None</td>
</tr>

Answer

I think you are just missing the trimming of extra space around obj!

$('tbody tr td .h5').parents('tr').each(function(){

        var obj = $(this).find('span.meter-description').html().replace(/ /g, "");
        console.log(obj);
        console.log('--');

        if($.trim(obj) == '0of0'){
            alert('hello');
        }

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <table>
    <tbody>
      <tr>
        <td>
          <div class="h5">
            </div>
          </td>
        <td>
          <span class="meter-description">
            0 of 0
            </span>
          </td>
        
        </tr>
      </tbody>
    </table>
</div>