Search code examples
jqueryattributescss-selectorschaining

JQuery : selector chain with attribute filter : result does not persist?


I am pulling my hair out!! grrr...

This seems to work:

//get all the foo and bar links that point to a named anchor:
$("a.foo,a.bar").filter("[href^=#]").click
(
    function()
    {
        doSomething( $(this).attr("href").substr(1) );
        return false;
    }
);

When I log the output of '$(this).attr("href").substr(1)' to the console I see the anchorname without the hash, for all the relevant links. All good. But inside doSomething(), typeof() of says I have a string - but it's empty!! Why?!!

function doSomething(str)
{
    //log str : it's empty!!
}

Usually soon after I post a question on here it's too bizarre for anyone to help me or I figure out my own stupidity later. I hope someone can help this time! :-(

============== SOLVED =====================

********* It was my own stupidity **********

inside doSomething the first thing that my code did was:

if(str = "") return;

(slaps forehead). Thank you all for making me stare at this in a different way


Solution

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