2

I'm trying to compare these codes to boolean value.

HTML

<a href="#" id="like">
    <i class="fa fa-thumbs-o-up fa-fw"></i>
</a>

jQuery

$('#like').on('click', function(event) {

    ...

    var htmlTag = '<i class="fa fa-thumbs-o-up fa-fw"></i>';
    var isLike = event.target.closest('#like').innerHTML == htmlTag;

    if (isLike) {
        console.log('true');
    } else {
        console.log('false');
    }

    ...

});

But it's going to false, In fact it should be true. Why? Thanks for every answers.

3
  • Why not try outerHTML instead? Commented Jun 2, 2017 at 12:43
  • The id attribute of an element should be unique for the whole page. This might be contributing to your woes. Also why the check at all? You could change the markup to target the i element or use a more specific selector to target it $('.like i') Commented Jun 2, 2017 at 12:49
  • Thanks for every idea.. It solved. Commented Jun 2, 2017 at 12:59

4 Answers 4

2

The problem is that the innerHtml is returning a string with extra spaces use trim() in order to fix it

var isLike = event.target.closest('#like').innerHTML.trim() == htmlTag;

$('#like').on('click', function(event) {
    console.log(event.target);
    var htmlTag = '<i class="fa fa-thumbs-o-up fa-fw"></i>';
    var isLike = event.target.closest('#like').innerHTML.trim() == htmlTag;


    if (isLike) {
        console.log('true');
    } else {
        console.log('false');
    }

});
#like{
height:50px;
width:50px;
border:1px solid;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="like">
    <i class="fa fa-thumbs-o-up fa-fw"></i>
</a>

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

1 Comment

my question it solved. But your answer also great! I'm unexpected to use .trim() Thanks for your answer!
1

You should use .html() method in order to return the html of hyperlink.

$('#like').on('click', function(event) {
    var htmlTag = 'Like<i class="fa fa-thumbs-o-up fa-fw"></i>';
    var isLike = $(this).html().trim() == htmlTag;
    if (isLike) {
        console.log('true');
    } else {
        console.log('false');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a  id="like">
  Like<i class="fa fa-thumbs-o-up fa-fw"></i>
</a>

Comments

1

Try this:

$('button').on('click', function(event) {
    var htmlTag = '<i class="fa fa-thumbs-o-up fa-fw"></i>';
    var isLike = $('#like').html();
    if (htmlTag.trim() === isLike.trim()) {
        console.log('true');
    } else {
        console.log('false');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
Test
</button>

<a href="#" id="like">
    <i class="fa fa-thumbs-o-up fa-fw"></i>
</a>

1 Comment

my question it solved. But your answer also great! I'm unexpected to use .trim() Thanks for your answer!
1

event.target.closest('#like').innerHTML returns you the html in the string format but its with white spaces appended to it. You can use $.trim() to remove these white spaces before string comparision.

$(document).ready(function() {
  $('#like').on('click', function(event) {

    var htmlTag = '<i class="fa fa-thumbs-o-up fa-fw">Hi</i>';
    alert("#" + event.target.closest('#like').innerHTML + "#");
    var isLike = $.trim(event.target.closest('#like').innerHTML) == htmlTag;

    if (isLike) {
      alert('true');
    } else {
      alert('false');
    }


  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<a href="#" id="like">
  <i class="fa fa-thumbs-o-up fa-fw">Hi</i>
</a>

1 Comment

Yep! My question it solved. But your answer also great! I'm unexpected to use $.trim(...) Thanks for your answer!

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.