1

I has an array of objects $('selector'), I want to get index() of each element and append it to the html of each elemt. I can get index of first element just write $('selector').index(), but how I can get other, and append them to they DOM-objects. Help, I am new in Javascript and jQuery.

asked Oct 20, 2014 at 11:11
2
  • You can use .each() Commented Oct 20, 2014 at 11:12
  • 1
    Thanks you guys, i appreciate yours help) Commented Oct 20, 2014 at 11:25

8 Answers 8

3

Try this...

$("selector").each(function(i) {
 $(this).html($(this).html() + " " + i);
});

i will be the index of each selected element and will be appended to the html.

Here's a jsfiddle example...

http://jsfiddle.net/54bcn68j/

answered Oct 20, 2014 at 11:13
Sign up to request clarification or add additional context in comments.

Comments

2

You can pass a function to the .html() jQuery method. This function will conveniently be called with two parameters, the element index and the current HTML, so what you want is easy:

$elements.html(function(i, v){
 return v + i; 
});

JSFiddle

Which will give you the index relative to the selection made. If you wanted the index relative to each elements siblings, you would need .index():

$('ul li').html(function(_, v){
 return v + $(this).index();
});

JSFiddle

answered Oct 20, 2014 at 11:14

2 Comments

@Archer Glad you think so. In fact, many jQuery methods support a function being passed to their setter versions.
Yeah, I was aware of that but it's something that never comes to mind. I've not seen it used in this particular way, but I like it!
0

Use JQuery.each:

$('selector').each(function(index) {
 console.log(index);
});

Example:

Html:

<div>My Div</div>
<div>My Div</div>
<div>My Div</div>
<div>My Div</div>
<div>My Div</div>
<div>My Div</div>
<div>My Div</div>
<div>My Div</div>
<div>My Div</div>

JS:

$('div').each(function(index){
 var newHtml = $(this).html() + " - Index:" + index;
 $(this).html(newHtml);
});

Output:

My Div - Index:0
My Div - Index:1
My Div - Index:2
My Div - Index:3
My Div - Index:4
My Div - Index:5
My Div - Index:6
My Div - Index:7
My Div - Index:8

JSFiddle.

answered Oct 20, 2014 at 11:14

Comments

0

Use .each() to select each element, get each index and append each index to all elements matching the selector.

$("selector").each(function(index) {
 var eachIndex = $(this).index();
 $(this).append(eachIndex);
});
answered Oct 20, 2014 at 11:14

Comments

0

Try:

$.each($('selector'), function() {
 $(this)append($(this).index());
});
answered Oct 20, 2014 at 11:16

Comments

0

You can also use eq:

$('selector').each(function(){
 $(this).html('index for this html is ' + $(this).eq());
});
answered Oct 20, 2014 at 11:18

Comments

-1

You should iterate using .each

Sample Code:

$("selector").each(function(i, v) {
 $(this).append(i);
});
answered Oct 20, 2014 at 11:14

Comments

-1

Just cycle through them and the iterator will tell you it's index.

var array = $('.someClass');
for(var i=0; i<array.length; i++){
 console.log('Element #' + i + ': ' + $(array[i]).text());
}
answered Oct 20, 2014 at 11:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.