I have following a tags:
<a href="#tab1">Any tab1 Label</a>
<a href="#tab2">tab2 Label</a>
<a href="#tab3">Any tab3 Label</a>
<script>
function tellMyName()
{
alert(this.href);
}
</script>
Now i want to bind the tellMyName function to all the a tags and get tab1 if Any tab1 Label is clicked, tab2 if tab2 Label is clicked and so on...
function tellMyName() {
alert(this.hash.substr(1));
}
$('a').click(tellMyName);
function tellMyName() {
var str = this.href.split("#")[1];
alert(str);
}
Not all the time only the hash part will be displayed on the link. Sometimes the Host is added to the href
. By splitting the href with the hash
(#) and get the second part of the split string, you'll get what you want.
You can do something like this
var fragment = $('a#my-link')[0].hash.substr(1);
Check it out.
function tellMyName() {
alert(this.hash.replace('#',''));
}
$('a').click(tellMyName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With