views:

43

answers:

6

hi guys,

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...

+1  A: 
<script>
function tellMyName()
{
    var text = this.href;
    text = text.replace("#","");
    alert(text);
}

</script>
Chinmayee
sorry i get the webaddress like http://localhost/xyz/ in front of it too..
KoolKabin
as suggested below, you have to bind this function to anchor tag. i.e. $('a').click(tellMyName);
Chinmayee
+2  A: 
function tellMyName() {
  alert(this.hash.substr(1));
}

$('a').click(tellMyName);
sje397
sorry i get the webaddress like localhost/xyz in front of it too..
KoolKabin
if should be this.hash.substr(1)
KoolKabin
@KoolKabin - edited. Ta.
sje397
+1  A: 
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.

rob waminal
yup its returning the correct one as i am searching... thnx and binding has been working as in http://stackoverflow.com/questions/3861076/how-can-i-extract-text-after-hash-in-the-href-part-from-a-tag/3861095#3861095
KoolKabin
A: 

Try this -

<a href="#tab1">Any tab1 Label</a>
<a href="#tab2">tab2 Label</a>
<a href="#tab3">Any tab3 Label</a>

<script type="text/javascript">
$('a').click(function(){
alert(this.hash.split('#')[1]); //This will alert # part in clicked anchor tag
});
</script>
Alpesh
+1  A: 
function tellMyName() {
  alert(this.hash.replace('#',''));
}

$('a').click(tellMyName);

crazy fiddle

Reigel
A: 

You can do something like this

var fragment = $('a#my-link')[0].hash.substr(1);

Check it out.

alex