jQuery: get only the hash value from links
by gabrieleromanato
HTML
<ul id="test">
<li>
<a href="#s1">A</a>
</li>
<li>
<a href="#s2">B</a>
</li>
<li>
<a href="#s3">C</a>
</li>
</ul>
JavaScript
(function($) {
$.fn.getHash = function() {
if (!this.is('a')) {
return;
}
return this.each(function() {
$(this).data('hash', $(this)[0].hash.replace('#', ''));
});
};
})(jQuery);
$('a', '#test').getHash();
$('a', '#test').each(function() {
var $a = $(this);
$a.parent().append('<strong>' + $a.data('hash') + '</strong>');
});