A: 

try substr(0) first character is at index 0 http://www.w3schools.com/jsref/jsref_substr.asp

antpaw
I think he's intentionally trying to omit the #.
Matchu
well i think you should still accept what i think
antpaw
...what does that mean? xD Input is always appreciated, but to appreciate input and say it's wrong are not mutually exclusive.
Matchu
+1  A: 

I can't reproduce this. Sounds like an issue with the doSomething code itself - do a quick code review to make sure you're pointing to the right function and that you're not modifying the argument before use.

Matchu
omg... thank you about the comment about 'modifying the argument'...I feel so stupid. I had done 'if(str = "") return; can you spot the problem? Yah...
rekindleMyLoveOf
A: 
$(document).ready(function(){
    $("a.foo,a.bar").filter("[href^=#]").click(function() {
        doSomething( $(this).attr("href").substr(1) );
        return false;
    });

    doSomething = function(str) {
        alert(str);
    }
});

Works for me using

 <a class="foo" href="#1111">1</a>
 <a class="bar" href="#2222">2</a>
sberry2A
thanks sberry2A. course it works.. I just goofed up. :)
rekindleMyLoveOf
A: 

note: This isn't the correct answer! But I'm going to leave it here in case it is of use to other Sizzle users.


It's a bug in Sizzle (the selector library used by jQuery).

The problem is when you access link.href in JavaScript you don't get the text of the href attribute, you get the resolved absolute URL. Naturally this begins with http:// and not #.

When you call link.attr('href') jQuery detects the special case and applies a workaround. When you use a selector, Sizzle doesn't. It tries:

attrHandle: {
    href: function(elem){
        return elem.getAttribute("href");
    }
},

but this workaround doesn't work on IE up to version 7, as they make getAttribute equal to just getting the JavaScript property. [The correct workaround would be to use getAttributeNode instead, or sniff IE and use the proprietary getAttribute('href', 2) extension.]

So the selector engine gets http://... for the href attribute and of course this never matches ^=#.

If you do it yourself with a filter function it should work:

$('a.foo, a.bar').filter(function() {
    return $(this).attr('href').substring(0, 1)==='#';
})...
bobince
Hmm... I hadn't heard of this before... thanks for the info!
rekindleMyLoveOf
Ah... looks like it's OK in this case. Sizzle does have this bug, but current versions of jQuery monkey-patch the offending function to fix it! [Line 2196 of jQuery 1.3.2.]
bobince