# Jquery, targetInnerHTML with CSS classes

**URL:** <https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627>\
**Category:** General\
**Created:** [November 6, 2019, 8:42am UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627 "2019-11-06T08:42:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [November 6, 2019, 8:42am UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/1 "2019-11-06T08:42:28Z")

</div>

Hey everyone. I am trying another go at a JS calculator but I’ve encountered a small problem

```auto
$(document).ready(function() { $('button').on('click', function(e) { let btn = e.target.innerHTML;        
  if (btn === $('.operators')) 
  { console.log('operator') } else {console.log('number');
                                                               }});});

```

I am trying to get btn to console.log operator and numbers separately, however it’s only giving me “number” no matter how I try to get the btn element to target it. I’m convinced i am just slightly off.  
Please lend your expertise to me, thank you.

---

<div class="post-metadata">

**Author:** ![simonebogni](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/simonebogni/32/266841_2.png) [@simonebogni](https://forum.freecodecamp.org/u/simonebogni)\
**Post date:** [November 6, 2019, 12:10pm UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/2 "2019-11-06T12:10:26Z")

</div>

Hi,  
what I understand from the code is that you have buttons and to each of them you are giving a click event listener.  
In case the event is triggered, for a given button you are getting the html text inside it and compare it to **an object** returned by the jQuery selector. This will always be evaluated as false because the object returned will never be identical to the string inside the button.  
If you want to see if a button had a class “operators”, you can do so like this:

```auto
$("button").click(function(){
  if($(this).hasClass("operators")){
    console.log("operator");
  } else {
    console.log("number");
  }
});

```

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [November 6, 2019, 10:52pm UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/3 "2019-11-06T22:52:42Z")

</div>

Yes, that’s exactly what I wanted. Thank you!

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [November 7, 2019, 2:42am UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/4 "2019-11-07T02:42:58Z")

</div>

I’m sorry I spoke too soon. I am still getting the same problem.  
My code is

```auto
$(document).ready(function() {
  $('button').on('click', function(e) {
    let selection = (e.target.innerHTML);
    console.log(selection); 
                                     });
   $("button").click(function(){
  if($(this).hasClass("operators")){
    console.log("operator");
  } else {
    console.log("number");
  }
});

```

Still no matter what I click I get “number”

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [November 7, 2019, 3:12am UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/5 "2019-11-07T03:12:55Z")

</div>

I have a temporary fix. I changed my operators to be `<a>` elements instead of buttons, then just did this.  
It’s a band-aid over the problem, so I am still curious as to make this better.

```auto
 $('button').on('click', function(e) {
  let btn = e.target.innerHTML; 
  console.log(btn)
  console.log("number")});
  $('a').on('click', function(e) {
    let btn2 = e.target.innerHTML;
    console.log(btn2)
    console.log("operator")
  });    

```

---

<div class="post-metadata">

**Author:** ![Imstupidpleasehelp](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@Imstupidpleasehelp](https://forum.freecodecamp.org/u/Imstupidpleasehelp)\
**Post date:** [November 7, 2019, 10:09am UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/7 "2019-11-07T10:09:52Z")

</div>

[https://codepen.io/Dylanbozarth/pen/jOOrNVQ?editors=1010](https://codepen.io/Dylanbozarth/pen/jOOrNVQ?editors=1010)  
here is a link the full file. Tried with your provided code and I’m still getting the same error.

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [November 7, 2019, 1:45pm UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/8 "2019-11-07T13:45:23Z")

</div>

Being able to see the full code is always better than just a snippet.

You should only have one class attribute per element. You can put all of the class names (separated by spaces) as the value of the attribute. Currently, only the first class is being recognized if there are multiple ones (as you currently have).

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 2:46pm UTC](https://forum.freecodecamp.org/t/jquery-targetinnerhtml-with-css-classes/325627/10 "2021-01-16T14:46:01Z")

</div>

Closed on January 16, 2021, 2:46pm UTC


