2

I am new to JavaScript and StackOverflow. I am trying to extract all he button elements from a page and then set an onclick attribute to them I have tried the following but it does not seem to work any help would be appreciated.

   var btn = [];
   for (var i=-1; i<btn.length; i++)
       {
           btn[i] =   this.document.getElementsByTagName("button")
             .setAttribute("onClick","btnClick()");
       }
   console.log("array length " + btn.length);
   console.log(btn);

In the console I get:

     script.js:13 Uncaught TypeError: this.document.getElementsByTagName(...)
            .setAttribute is not a function at script.js:13
     (anonymous) @ script.js:13

I have tried a number of different things, if I remove the .setAttribute from the code I get:

    script.js:15 array length 0
    script.js:16 
    [-1: HTMLCollection(13)]
    -1: HTMLCollection(13)
    board:button#board.btn
   edCourse:button#edCourse.btnGray1
   edModule:button#edModule.btnGray1
   edStudent:button#edStudent.btnGray1
   entCourse:button#entCourse.btnGray
   entModule:button#entModule.btnGray
   entStudent:button#entStudent.btnGray
   length:13
   resultLst:button#resultLst.btn
   sap:button#sap.btn
   trans:button#trans.btn
   vwCourse:button#vwCourse.btnGray
   vwModule:button#vwModule.btnGray
   vwStudent:button#vwStudent.btnGray
   0:button#entStudent.btnGray
   1:button#entCourse.btnGray
   2:button#entModule.btnGray
   3:button#edStudent.btnGray1
   4:button#edCourse.btnGray1
   5:button#edModule.btnGray1
   6:button#vwStudent.btnGray
   7:button#vwCourse.btnGray
   8:button#vwModule.btnGray
   9:button#sap.btn
   10:button#board.btn
   11:button#trans.btn
   12:button#resultLst.btn
   __proto__:HTMLCollection
   length:0
   __proto__:Array(0)

Please help, sorry if this question has been asked before, but I couldn't find an answer.

2 Answers 2

2

document.getElementsByTagName returns an array-like HTMLCollection. No need for this.document, by the way. Just use document.

Iterate over this collection and bind event listeners using addEventListener:

var btn = document.getElementsByTagName("button");
for (var i = 0; i < btn.length; i++) {
  // btn[i] contains the <button> element
  btn[i].addEventListener('click', btnClick);
}
console.log("array length " + btn.length); // logs the number of buttons in the document
console.log(btn); // logs the HTMLCollection

The snippet above requires a function called btnClick, which is called whenever one of the buttons is clicked.

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

1 Comment

Thank you for the quick reply this works exactly as I wanted
1

You have several syntax errors.

1) Array indeces should start from 0. Not with -1.

2) Your btn array is empty which shouldn't be.

3) You should access the current element with index. Not with this

   var btn = document.getElementsByTagName("button");
   for (var i=0; i<btn.length; i++)
       {
           btn[i].setAttribute("onClick","btnClick()");
       }
   console.log("array length " + btn.length);
   console.log(btn);

With complete code it should

 var btn = document.getElementsByTagName("button");
   for (var i=0; i<btn.length; i++)
       {
           btn[i] =   btn[i].setAttribute("onClick","btnClick()");
       }
   console.log("array length " + btn.length);
   console.log(btn);
   
   
   function btnClick(){
    alert("clicked");
   }
<button>b1</button>
<button>b2</button>

5 Comments

There is no need to assign the btn[i] inside the loop. In fact it is more or less a lucky coincidence that doing so does not put unwanted stuff in the btn array.
@PeterB Yes. There is no need to do that. As they are references anyway.
Not because the are references, but because HTMLCollections are live and can't be modified from outside. Anyway, it's better to use addEventListener.
@PeterMader That is what a reference mean. And It would be great how using addEventListener good than onclick attribute.
Take a look at this and this SO answers. There are many reasons not to use inline event handlers.

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.