sonal Posted April 4, 2012 Share Posted April 4, 2012 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 More sharing options...
Larry Posted April 5, 2012 Share Posted April 5, 2012 Could you show the relevant HTML? Link to comment Share on other sites More sharing options...
HartleySan Posted April 5, 2012 Share Posted April 5, 2012 Your JS looks find, so I have to assume that there's something up with your HTML (like there are no button elements). Link to comment Share on other sites More sharing options...
sonal Posted April 5, 2012 Author Share Posted April 5, 2012 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" ?) 1 Link to comment Share on other sites More sharing options...
HartleySan Posted April 6, 2012 Share Posted April 6, 2012 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!). 1 Link to comment Share on other sites More sharing options...
sonal Posted April 6, 2012 Author Share Posted April 6, 2012 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 More sharing options...
Recommended Posts