admin管理员组

文章数量:1419248

I am very new to this, but I wrote this and thought it would work first time, but I get an 'ReferenceError: Can't find variable: street' console error.

I get this error if I click on the 'Street' button.

It's quite basic but this is the first time I've made a function to use a var/ref from the onClick attribute.

Please see onClick markup...

<a href="#" title="View Supersport" onclick="bikeFilter(supersport)">Supersport</a>

<a href="#" title="View Street" onclick="bikeFilter(street)">Street</a>

<a href="#" title="View Cruiser" onclick="bikeFilter(cruiser)">Cruiser</a>

<a href="#" title="View Scooter" onclick="bikeFilter(scooter)">Scooter</a>      


<a href="#" title="Motocross" onclick="bikeFilter(motocross)">Motocross</a>

<a href="#" title="Enduro" onclick="bikeFilter(enduro)">Enduro</a>

<a href="#" title="Kids" onclick="bikeFilter(kids)">Kids</a>

then please see my function, which gets the ref error above...

Also please note I am trying to use the onClick var/ref within my function so I can target specific elements relative to the button being clicked.

bikeFilter = function (y) {

    $('.bike').fadeOut();

    scrollTo(186);

    $('.bike[data-group=' + y + ']').fadeIn();

    bikeSliderNav();

    bikeSlider();

    return false;

}


Any expert advice would be really helpful.

Thanks in advance.

I am very new to this, but I wrote this and thought it would work first time, but I get an 'ReferenceError: Can't find variable: street' console error.

I get this error if I click on the 'Street' button.

It's quite basic but this is the first time I've made a function to use a var/ref from the onClick attribute.

Please see onClick markup...

<a href="#" title="View Supersport" onclick="bikeFilter(supersport)">Supersport</a>

<a href="#" title="View Street" onclick="bikeFilter(street)">Street</a>

<a href="#" title="View Cruiser" onclick="bikeFilter(cruiser)">Cruiser</a>

<a href="#" title="View Scooter" onclick="bikeFilter(scooter)">Scooter</a>      


<a href="#" title="Motocross" onclick="bikeFilter(motocross)">Motocross</a>

<a href="#" title="Enduro" onclick="bikeFilter(enduro)">Enduro</a>

<a href="#" title="Kids" onclick="bikeFilter(kids)">Kids</a>

then please see my function, which gets the ref error above...

Also please note I am trying to use the onClick var/ref within my function so I can target specific elements relative to the button being clicked.

bikeFilter = function (y) {

    $('.bike').fadeOut();

    scrollTo(186);

    $('.bike[data-group=' + y + ']').fadeIn();

    bikeSliderNav();

    bikeSlider();

    return false;

}


Any expert advice would be really helpful.

Thanks in advance.

Share Improve this question asked Nov 4, 2012 at 19:19 JoshcJoshc 3,86316 gold badges81 silver badges125 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You'd probably wanna pass a String as input to your function and not the name of an undeclared and uninstantiated variable.

Try to use the single quotes to refer it as a String constant (you need single quotes since you are already using double quotes to tell your html tag the attribute value):

onclick="bikeFilter('scooter')"

Take a look here to see the difference in data typing in js, and here for a quick start about functions.

You should use them like :

onclick="bikeFilter('motocross')"

Don't forget to put ' around your parameters

本文标签: javascriptrun jQuery function by inline onClick and use function variablereferenceStack Overflow