There is a button, the button has an id , this button is present on every page, but on different pages, it has different id . For example, on the first page of her id buttonsd123 on another buttonsd213 . How to make the correct var , so that it buttonsd on buttonsd and what is no longer important.

Like that:

 var knopka = document.getElementById('buttonsd//и тут уже любые значения') 

There are more than 1000 pages, because in each one there’s no need to write id buttons

  • Is jQuery on the page? - Michael Vaysman Sep.
  • Hi, jQuery is not. - user191380

2 answers 2

http://htmlbook.ru/css/selector/attr3
Example link: http://codepen.io/Besmer/pen/qOqqzE
Just implementation:

 document.querySelector('button[id^=buttonsd]') 
  • Can you please describe in more detail? not working ... But thanks! - user191380
  • Strange, it should work. Check the querySelector support in your browser, but it should be in all browsers at the moment. If it does not work, then connect jquery / analogs or use a less optimized, but working next answer. - Yegor Nikitenok

If no jQuery is like this:

 var elems = []; var buttons = document.getElementsByTagName("button"); for(var i = 0; i < buttons.length; i++) { if(buttons[i].id.indexOf('buttonsd') == 0) { elems.push(buttons[i]); } } 

The elems will contain the button objects on the page. Ie if there is one button on each page, then in elems there is that button.