Modify list counter in :before pseudo class with jQuery using start attribute

Suppose you have two ordered list ol tags, and the second one has a start attribute. You also have in…

April 20, 2016

Suppose you have two ordered list ol tags, and the second one has a start attribute.
You also have in css a style on li:before to style the number:

ol li:before {
	content: counter(item);
 }

If in the HTML you have two separate ol tags and the second with “start” attribute, it will not work, because content: counter(item) will start from 1 again.

To fix it you can use this javascript trick using jQuery to add apply start to the :before pseudo class element:

$('ol').each(function(){
	if($(this).attr("start")) {
		var $q = parseInt( $(this).attr("start") );
		if($q>0) $q--;
		$(this).find("li:first").each(function(){
			$(this).css("counter-reset","item " + $q);
		});
	}
});

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

How to write a text description into html input type password

Sometime designers put form labels and instrucions into html inputs. One of the common uses is for login boxes when…

March 10, 2010

Mini gallery/slideshow with PHP and JQuery

Sometimes I’ve had to quickly put in a page a simple slideshow, the first times I’ve searched a lot around…

December 13, 2009

Set “write here” on input type text?

If you are using JQuery framework and you want to set up the default value of some text box in…

November 17, 2009

Add Internet Explorer class to body to detect old browser

When you have clients that still uses an old browser, you need to handle it. This javascript function detects the…

February 27, 2017

Limit the number of categories for posts in WordPress

CHOOSE ONLY ONE CATEGORY WORDPRESS If you need to limit the number of categories used by the authors of your…

September 14, 2015

Scraping content with PHP as if it was jQuery

Building a spider or a bot needs some knowledge of regular expressions, you must know and use preg_match or preg_match_all…

December 8, 2013