3 I have the following jquery var replaceit = $(this)....">

Changing values โ€‹โ€‹with jQuery

I have the following:

<span class="label-info">3</span> 

I have the following jquery

 var replaceit = $(this).closest(':has(.label-info)').find('.label-info').text(); 

The value of the variable is always a single integer, but not always 3:

 ie: 1, 2, 3, 4, 5. 

I have tried these many ways and cannot change the meaning. My last attempt:

 return $(this).closest(':has(.label-info)').html().replace(replaceit, (replaceit - 1)); 

My end result is to subtract 1 from any current "lable-info" value and switch it with this new result. Thus, a new range based on a value of 3 will become.

 <span class="label-info">2</span> 

How do I achieve this?

Updated code for clarity.

HTML:

 <div> <span class="lable-info">3</span> </div> <div> <a class="accept_friend">accept</a> </div> 

JavaScript:

  $(document).on("click", "a.accept_friend", function() { var checkValue = $(this).closest(':has(.name)').find('.name').text(); var removeit = $(this).closest(':has(.item)').find('.item').fadeOut(); var replaceit = $(this).closest(':has(.label-info)').find('.label-info').text(); $.ajax({ url: '/includes/accept_friend.php', type: 'post', data: {checkValue}, success:function(data){ return removeit; $("a.remove_pending").text(function () { return ('.label-info').html().replace(replacei, replaceit); }); } 

Notes:

I do not use id. I am using a class. There are several classes with the same name. So I have to call closer.

+5
source share
4 answers

Getting a value from a range, subtracting it by 1 and updating the range (using jQuery):

HTML

 <span class="label-info">3</span> 

JQuery

 var n = $(".label-info").text(), n2 = n - 1; $(".label-info").text(n2); 

Hope this helps a bit

+4
source

Please try below code

 var currentVal = $(this).closest(':has(.label-info)').html(); var newValue = parseInt(currentVal - 1); return newValue; 
+3
source

Hope this helps you

 var replaceit = $(this).closest(':has(.label-info)').text(); $(this).closest(':has(.label-info)').text(replaceit-1); 

Script example https://jsfiddle.net/nzdtnoas/

0
source

You can use .text( function ) to change the text of an element to another line.

 $(".label-info").text(function(i, text){ return text - 1; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="label-info">3</span> 
0
source

All Articles