0

var allp = $("div p");
for (var i = 0; i < allp.length; i++) {
  allp.attr("class", function(i, n) {
    n += 1;
    return n;
  });
  console.log(allp[i]);
}
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<div>
  <p class="1">1</p>
  <p class="2">2</p>
  <p class="3">3</p>
  <p class="4">4</p>
  <p class="5">5</p>
</div>

i means index, n means the current value of "class". Just wanted to make each classname +1,but failed to do that.And the console.log is :

<p class="11111">1</p>  
<p class="21111">2</p>  
<p class="31111">3</p>      
<p class="41111">4</p>  
<p class="51111">5</p>

Expected output:

    <p class="2">1</p>  
    <p class="3">2</p>  
    <p class="4">3</p>      
    <p class="5">4</p>  
    <p class="6">5</p>
Chor
  • 833
  • 1
  • 5
  • 14

5 Answers5

0

Maybe:

var allp = $("div p");
for (var i = 0; i < allp.length; i++) {
    allp.attr("class", function (i, n) {
        const n2 = Number(n) + 1;
        return n2;
    });
    console.log(allp[i]);
}
German Burgardt
  • 375
  • 1
  • 5
0

Use jQuery.each() to iterate through each element in $allp, then .attr() to set the new class name.

var $allp = $("div p")

$.each($allp, (index, el) => {
  const $newEl = $(el).attr('class', `newClass-${index + 2}`)
  console.log($newEl[0])
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<div>       
  <p class="1">1</p>    
  <p class="2">2</p>    
  <p class="3">3</p>        
  <p class="4">4</p>    
  <p class="5">5</p>
</div>

Codepen

ksav
  • 20,015
  • 6
  • 46
  • 66
0

Simply ignore for loop:

var allp=$("div p");    
allp.attr("class",(i,n)=>Number(n)+1);
console.log(allp)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>       
  <p class="1">1</p>    
  <p class="2">2</p>    
  <p class="3">3</p>        
  <p class="4">4</p>    
  <p class="5">5</p>
</div>
FZs
  • 16,581
  • 13
  • 41
  • 50
  • the for loop here is unnecessary? – Chor Jan 25 '19 at 16:45
  • @user10968312 You can do it by `for` loop, but it's more simple without it. Additionally, if you make a loop and pass a function to `.attr()`, then your code will be processed multiple. – FZs Jan 25 '19 at 17:07
  • But if I dont use for loop ,what will the i mean here?It should be index when using for loop – Chor Jan 25 '19 at 17:11
  • @user10968312 `i` is passed to the function that processes the 'reclassing'. [It's the index](http://api.jquery.com/attr/#attr-attributeName-function), but it isn't used in this example. – FZs Jan 25 '19 at 17:13
  • By passing i and n without for loop, the function can still operate each p's classname,right? – Chor Jan 25 '19 at 17:17
  • @user10968312 Yes, if you pass a function to `.attr()`, it will *loop over* each element as a `for` loop do, but this way is more simple to read and use. – FZs Jan 25 '19 at 17:20
0

You just need to get the class attribute value of each p element increment it after converting it to a Number and then assign this value to the same element as the new text value.

var allp=$("div p");    
for(var i=0;i<allp.length;i++){     
  $(allp[i]).text(Number($(allp[i]).attr("class")) + 1);
console.log(allp[i]);   
}
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<div>       
  <p class="1">1</p>    
  <p class="2">2</p>    
  <p class="3">3</p>        
  <p class="4">4</p>    
  <p class="5">5</p>
</div>
Deep
  • 9,594
  • 2
  • 21
  • 33
  • I forgot to post the expected output before.I mean changing the classname instead of changing the content of p – Chor Jan 26 '19 at 02:37
-1

You can use jquery. Get the class, parse it to int and increment. It'll throw an error if you have none numeric characters in your class though so may want to do a check !isNaN.

$('div p').each(function(){
    console.log(parseInt($(this).attr('class')) + 1);
});
Sean T
  • 2,414
  • 2
  • 17
  • 23