2

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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for those links, I will take a look and learn. Nice one!
That tutorial is easy to follow but pretty much complete : )
1

You should use them like :

onclick="bikeFilter('motocross')"

Don't forget to put ' around your parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.