Jump to content
Larry Ullman's Book Forums

Getelementsbytagname Method


Recommended Posts

I have 3 buttons on my html document. I'm writing following code to get their reference. But the buttonArray length shows 0 all the time. What am I doing wrong?

 

 

 

window.onload = function() {

'use strict';

var buttonArray = document.getElementsByTagName('button'); //to reference all buttons on the document.

console.log(buttonArray.length);

for(var i = 0, count = buttonArray.length; i<count; i++) {

buttonArray.onclick = clickButton;

}

Link to comment
Share on other sites

Larry and HartleySan,

 

Thanks a lot for taking time here. Yes, there was problem in HTML. I have input buttons, and they are no button elements. I read something about that and changed the above code like this:

 

var buttonArray = document.getElementsByTagName('input');

console.log(buttonArray.length);

for(var i = 0, count = buttonArray.length; i<count; i++){

if (buttonArray.type.toLowerCase() == "button"){

console.log(buttonArray.name);

 

buttonArray.onclick = clickButton;

}

 

So, now I think that I also have to go back and learn, HTML!!!!!! When will I be able to get upgraded from beginner to "Professional?" (or atleast, "intermediate" ?)

  • Upvote 1
Link to comment
Share on other sites

Well, even "pros" make silly mistakes, but I think what separates pros from amateurs is a variety of things:

1) Professionalism (being able to get the job done, regardless of how little you actually want to do it)

2) Many years in the field with lots of personal experience and practice

3) The drive and desire to always learn more, and play around with different specs to really understand how things work

4) Listening to people (on forums) when they try to give you some good, friendly advice (*ahem*)

 

If you meet those requirements, then I think you're a pro (and I'm not implying that I am). I do, however, want to be a pro someday, as the whole Web technology movement greatly interests me and I spend way too much time these days focusing on it (like hanging around these forums as work!).

  • Upvote 1
Link to comment
Share on other sites

So nice of you to share such fine thoughts. I'm trying to learn as much as I can as this web technology interests me too. I'll always remember your points as I keep practicing and playing around with different aspects of programming and languages.

Thanks...

Link to comment
Share on other sites

 Share

×
×
  • Create New...