Skip to content

Instantly share code, notes, and snippets.

@bootstraponline
Created June 7, 2012 23:09
Show Gist options
  • Save bootstraponline/2892268 to your computer and use it in GitHub Desktop.
Save bootstraponline/2892268 to your computer and use it in GitHub Desktop.
CSS selectors in Chrome
/*
Bootstrap Online LLC. Released under the Apache license.
https://www.apache.org/licenses/LICENSE-2.0
Print CSS selector for the provided element.
Example using the currently selected item in Chrome Dev Tools:
c( $0 );
*/
function c( element ) {
var result = '';
var space = /\s/g;
var target = element;
while( target ) {
var id = target.id + '';
if ( id !== '' && id !== 'undefined' ) {
result = ' #' + id.replace( space, '.' ) + result;
} else {
var list = target.classList + '';
if ( list !== '' && list !== 'undefined' ) {
result = ' .' + list.replace( space, '.' ) + result;
}
}
var parent = target.parentNode;
target = parent ? parent : false;
}
// Ensure only one period per item.
return result.replace(/\.+/g,'.').trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment