Created
June 7, 2012 23:09
-
-
Save bootstraponline/2892268 to your computer and use it in GitHub Desktop.
CSS selectors in Chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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