Javascript Element How to - Create array of DIV's with specific ID tag








Question

We would like to know how to create array of DIV's with specific ID tag.

Answer


<!--from   www.java  2 s . co m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){
    var arr = $('div[id^="refresh"]').map(function(){ return this.id;}).get().join(',');
    document.writeln(arr);
});
</script>
</head>
<body>
  <div id='refresh5'></div>
  <div id='refresh12'></div>
  <div id='red'></div>
</body>
</html>

The code above is rendered as follows: