简体   繁体   中英

Increment an element's content (an efficient one-liner?)

I got a div which I want to increment the value of. It starts with the content "1", then it should get incremented to "2", "3", etc. by JS. I want to increment it in a nice way, not like this ugly line:

$popup_element.parents('.plussoie').find('.vote_counter').html(parseInt($popup_element.parents('.plussoie').find('.vote_counter').html()) + 1);

I don't want to have any variables, and I want a one line solution. I had the beginning of a solution, but it didn't work:

$popup_element.parents('.plussoie').find('.vote_counter').text(parseInt($(this.html()) + 1);

Fiddle

So can I increment a value easily, without the code soup above?

The power of vanilla JS :

element.textContent++;

Or, if you want you can use .innerHTML instead of .textContent . Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM