0

I'm trying to create a calculator out of javascript to work on my skills. I've added the class num to all of my buttons that have a number.

I'm trying to display to display the innerHTML of those buttons in the console when I click them with this code:

var num = document.getElementsByClassName('num');
num.addEventListener('click', getNum);

function getNum(){
    console.log(num.innerHTML);
}

getNum();

However all I get is

num.addEventListener is not a function.

Here is my codepen: https://codepen.io/teenicarus/pen/wrEzwd

what could I be doing wrong?

2
  • 1
    getElementsByClassName - note "elementS" there - this is plural form. This means you do not get one or null, but you get 0...n items. Which leads us to result, that function is returning array (Or NodeList exactly), not one item. And arrays cannot listen to events :-) Commented Oct 13, 2017 at 11:16
  • getElementsByClassName returns an array of the elements or tags which have the same class name. So be careful. Commented Oct 13, 2017 at 11:21

6 Answers 6

3

You need to change the code like below. getElementsByClassName returns collection of elements. Loop through the elements and add click event listener. In getNum, you can use this to get access to the button clicked.

var num = document.getElementsByClassName('num');

for (var i = 0; i < num.length; i++) {
        num[i].addEventListener('click', getNum);
}

function getNum(){
    console.log(this.innerHTML);
}

You can also use Array forEach like the following:

[].forEach.call(num, function(el){
  el.addEventListener('click', getNum);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense. Can it be done use forEach? Just wondering. If yes: will it be better?
2

getElementsByClassName returns a collection of elements, not a single element. If you want to get single element assign it an id attribute and use getElementById. This way you can use addEventListener function

Comments

1

Here's a solution you can plug directly in your codepen:

var nums = document.getElementsByClassName('num');
[].forEach.call(nums, num => num.addEventListener('click', numClick));

function numClick(){
    // adding + turns the text into an actual number
    console.log(+this.innerHTML);
}

getElementsByClassName() returns an HTMLCollection, to iterate over it you can pass it to [].forEach.call() like I showed above.

I also renamed the handler to numClick, since it doesn't "get" the number. And added +, which is a nice shortcut to turn text into a number (otherwise, adding two numbers would yield unexpected results, like "1" + "2" => "12"

Comments

0

The .getElementsByClassName returns not an element, but a collection of them. You can access elements using .getElementsByClassName(num)[element's sequential number], or better use id's and getElementById method.

1 Comment

You mean like writing out 10 separate but almost identical commands? That's not really good advice here.
0

Here is the modified code for your desired output.just copy and try:

var num = document.getElementsByClassName('num');
//num.addEventListener('click', getNum);
for (var i = 0; i < num.length; i++) {
   num[i].addEventListener('click', getNum);
}
function getNum(){

  document.getElementById('result').innerHTML+=this.innerHTML; 
    console.log('value:'+this.innerHTML);
}

//getNum();

Comments

0

As you tagged Jquery to your question I suppose that you are able to use Jquery as well. You can grab the clicked element's class and referance it with 'this' to get its text.

    $('.num').click(function(){
      var x = $(this).text();
      console.log(x);
    });

This is a working example you can check the console.log DEMO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.