Suppose there is a page, but the width in javascript is given only to the first. How to make the style ask all the element at once with some Id?

<html> <head> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type="text/javascript"> function Div() { var W = window.innerWidth - 200; var H = window.innerHeight - 50; var WMess = window.innerWidth - 280; document.getElementById('mess').style.width = WMess + "px"; } </script> </head> <body onLoad="Div()"> <div> <div id='mess'>Далинин Станислав</div> </div> <div> <div id='mess'>Далинин Станислав</div> </div> </body> </html> 
  • @ Kirpich643 Delete the extra code from the question, otherwise the probability of getting an answer will fall. - Nicolas Chabanovsky
  • 7
    Do you understand how silly it sounds "to all elements with one id"? The id should be unique on the page! - Specter
  • Well, offer your version! - Kirpich643

3 answers 3

it seems like I’ve never got anywhere:

 <body onLoad="Div()"> <div> <div class='mess'>Далинин Станислав</div> </div> <div> <div class='mess'>Далинин Станислав</div> </div> </body> function Div() { var W = window.innerWidth - 200, H = window.innerHeight - 50, WMess = window.innerWidth - 280; divs = document.getElementsByTagName('div'); for(var i=0; i < divs.length; i++) { if((new RegExp('mess').test(divs[i].className))) { divs[i].style.width = WMess + "px"; } } } 
  • one
    If not counting the bike where ordinary people use getElementsByClassName, then yes, it’s not messed up :) - Next_Alex
  • @Next_Alex bike, not a bike, and you see about cross-browser compatibility with your ear or ear? - lampa
  • If we assume IE browser, then yes. Although from the 9th version, he, too, finally approached the standards and even began to support getElementsByTagName. - Next_Alex 3:51
  • If cho, getElementsByTagName IE from version 6 supports. With connection :-). And those who do not consider IE as the browser, lose the most part of users. - lampa
  • one
    reinventing the wheel because of MS is also not ice. use jquery =) - thunder

It is understood that id is a unique element identifier for this page. If you need to create several elements of the same type, the class attribute is used.

    use class and jquery

    and read about the foreach function and walk around ALL the elements with id=mess

    id is the ID and it MUST be unique.