Reputation: 449
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
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:
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
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
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