-1

I have these divs

<div id="main">
  <div></div>
  <div></div>
</div>

and I want to somehow use javascript to automatically read how many divs are inside #main and also label them numerically. The output should be

<div id="main">
  <div class="1"></div>
  <div class="2"></div>
</div>
user1636946
  • 137
  • 1
  • 3
  • 14
  • 1
    question you should be asking yourself is `why?`. those classes have no semantic meaning. – chovy Feb 26 '14 at 03:00
  • 1
    It would be nice for you to show that you had made some effort by showing what you had tried (i.e. the code), along with stating the problem that you had and whenever possible include a [jsFiddle](http://jsfiddle.net) of your simplified problem. – Xotic750 Feb 26 '14 at 03:24
  • 1
    If you are going to accept an answer with jQuery in future, please include the jQuery tag otherwise people who obey the tags will provide answers in pure javascript. – acarlon Feb 26 '14 at 03:33

6 Answers6

1

No need for jQuery:

var divs = document.getElementById("main").getElementsByTagName("div");
for (var i = 0; i < divs.length; i += 1) {
    divs[i].setAttribute('class', i + 1);
}
divs.length; // this is the number of div children.
Roger
  • 2,912
  • 2
  • 31
  • 39
1

You can do

var el = document.getElementById('main'),
    count = 0;
for (var i = 0; i < el.children.length; i++) {
    if (el.children[i].nodeName == 'DIV') {
        el.children[i].className = ++count;
    }
}

Demo: Fiddle


If you have jQuery in your page, it should be as simple as

$('#main > div').addClass(function (i, clazz) {
    return '' + (i + 1)
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

First: 1, 2, 3, are not valid class names. See here.

a name must begin with an underscore (_), a dash (-), or a letter(a–z), followed by any number of dashes, underscores, letters, or numbers.

Second: 'class' is a future reserved word. Use className instead.

Here is an example using pure javascript and legal css names: jsfiddle

css:

.foo1
{
    color:blue;    
}
.foo2
{
    color:red;    
}

Html:

<div id="main">
  <div>1</div>
  <div>2</div>
</div>

Javascript:

var elem = document.getElementById("main");
var childDivs = elem.getElementsByTagName("div");
for (var i = 0; i < childDivs.length; i++) 
{        
    childDivs[i].className = 'foo' + (i+1).toString();
}
Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94
  • A further alternative would be to use the [`data`](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes) attribute, if HTML5 will be the only supported environment. – Xotic750 Feb 26 '14 at 03:29
-1

For this : I want to somehow use javascript to automatically read how many divs are inside #main . You might use :

$("#main").find("div").length

& for adding clas part :

$("#main").find("div").each(function(index1) {
   $(this).addClass(parseInt(index1) + 1) ;
});
Makrand
  • 587
  • 5
  • 14
  • 2
    JavaScript (not to be confused with Java) is a dynamically-typed language commonly used for client-side scripting. Use this tag for questions regarding ECMAScript and its dialects/implementations (excluding ActionScript). Unless a tag for a framework/library is also included, a pure JavaScript ... – Xotic750 Feb 26 '14 at 03:15
  • 1
    @Xotic750 agreed. I should have included the info that jQuery library is being used. But apparently a non-javascript answer has been accepted. Probably that's what the OP wanted. Unnecessary downvote. – Makrand Feb 26 '14 at 03:46
  • http://www.w3.org/TR/CSS21/syndata.html#characters Seems a very necessary downvote to me, and more of these answers should also be downvoted. `In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item).` – Xotic750 Feb 26 '14 at 03:55
  • And further, why the `parseInt`? jQuery's `each` passes the `index` to the function as a `number` See the [`API`](https://api.jquery.com/jQuery.each/) – Xotic750 Feb 26 '14 at 04:02
  • And why use `find` when you could have combined the CSS selector? – Xotic750 Feb 26 '14 at 04:03
-1

Use Jquery like this

$('#main div').each(function(i, o) {
    $(o).addClass(i+1)
})
jansesun
  • 108
  • 5
  • 2
    JavaScript (not to be confused with Java) is a dynamically-typed language commonly used for client-side scripting. Use this tag for questions regarding ECMAScript and its dialects/implementations (excluding ActionScript). Unless a tag for a framework/library is also included, a pure JavaScript ... – Xotic750 Feb 26 '14 at 03:17
-2
var DivCount = 0;

$("#main").find("div").each(function(index,value){

      DivCount = DivCount+1;
      $(value).attr("class",DivCount);
});
Arturo
  • 3,066
  • 2
  • 18
  • 11
  • 2
    JavaScript (not to be confused with Java) is a dynamically-typed language commonly used for client-side scripting. Use this tag for questions regarding ECMAScript and its dialects/implementations (excluding ActionScript). Unless a tag for a framework/library is also included, a pure JavaScript ... – Xotic750 Feb 26 '14 at 03:15