How to calculate button counts of a HTML page? -


i have html page, source code given below -

<html> <script type="text/javascript"> function jsmeth() {         alert('count of buttons : '+document.getelementsbytagname('button').length); } </script> <input type="button" value="button 1" id="btn1" name="btn1" /><br> <input type="button" value="butto 2" id="btn2" name="btn2" /><br><br> <input type="button" value="calculate button count" id="btn3" name="btn3" onclick="jsmeth();"/> </html> 

my basic purpose is, when click on button having id "btn3", should display number of buttons in html page (in case 3). somehow showing 0.


thanks quick reply. of suggested, have modified code shown below -

<html> <script type="text/javascript"> function jsmeth() {         alert('count of buttons : '+document.queryselectorall('[type=button]').length); } </script> <input type="button" value="button 1" id="btn1" name="btn1" /><br> <input type="button" value="butto 2" id="btn2" name="btn2" /><br><br> <input type="button" value="calculate button count" id="btn3" name="btn3" onclick="jsmeth();"/> </html> 

but getting below error -

message: object doesn't support property or method line: 4 char: 9 code: 0 uri: file:///c:/documents%20and%20settings/556996/desktop/demohtml.html 

i testing in ie8. please help!

thanks, kartic

use this...

function jsmeth() {         alert('count of buttons : '+document.queryselectorall('[type=button]').length); } 

or old browsers

function jsmeth() {     var elem = document.getelementsbytagname('input');     var numbts = 0;     for(i = 0; < elem.length; i++){         if(elem[i].getattribute('type') === 'button'){            numbts+=1;          }     }     alert(numbts); } 

demo


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -