Olèp
Olèp

Reputation: 449

Unexpected identifier - how functions works

I'm trying to create a small simple jquery function but i can't understand how to solvethis problem and can't find simple resources online where to read how to create simple functions with two variables. My code is

jQuery(function ($) {
    function infoText(menuitem,textinfo) {
        menuitem.click(function(){
            textinfo.fadeIn(300);
        }
    }

    infoText('.come-realizzo','#come-realizzo');
    infoText('.works','#works');

});

When i call the function i get this error:

Uncaught SyntaxError: Unexpected identifier on 9 line ( infoText('.come-realizzo','#come-realizzo'); ).

Thanks!

Upvotes: 0

Views: 148

Answers (3)

Irvin Dominin
Irvin Dominin

Reputation: 30993

You are missing a closing paren at the end of the click handler function.

You actual will not work because you are not referencing a jQuery object in the infoText function, two possible solutions:

  • pass jQuery objects as params
  • from passed string get the jQuery object

First:

jQuery(function ($) {
    function infoText($menuitem,$textinfo) {
        $menuitem.click(function(){
            $textinfo.fadeIn(300);
        });
    }

    infoText($('.come-realizzo'),$('#come-realizzo'));
    infoText($('.works'),$('#works'));

});

Second:

jQuery(function ($) {
    function infoText(menuitem,textinfo) {
        $(menuitem).click(function(){
            $(textinfo).fadeIn(300);
        });
    }

    infoText('.come-realizzo','#come-realizzo');
    infoText('.works','#works');

});

Upvotes: 0

Anton
Anton

Reputation: 32591

You are passing a string, you need to convert them into a jquery object using $(/*selector*/) like this below

Change

menuitem.click(function () {
  textinfo.fadeIn(300);
}

to

$(menuitem).click({
  $(textinfo).fadeIn(300);
});
 ^ missing to close the click function

Upvotes: 2

sino
sino

Reputation: 776

You are passing string instead of identifier ,use the below code

infoText($('.come-realizzo'),$('#come-realizzo') );

OR this other solution if you want to pass text instead of selector to your function

  $( menuitem).click(function(){
  $( textinfo).fadeIn(300);

Upvotes: 0

Related Questions