tags:

views:

27

answers:

3

I am passing string to a function but its giving me an error :

assignAttr(var menu="menu", var container="menu_container",var text="menu_text");

function assignAttr(menu,container,text)
{
 alert(menu + container + text);
}
+2  A: 

Proper code is like this:

function assignAttr(menu,container,text) { 
  alert(menu + container + text); 
}

var menu="menu";
var container="menu_container";
var text="menu_text";
assignAttr(menu, container, text);

The code serves the purpose of display how to use a variable as a parameter.

And of course you can write function call as:

assignAttr("menu", "menu_container", "menu_text");
MartyIX
+4  A: 

You simply need to call the function like this:

assignAttr("menu", "menu_container", "menu_text");
casablanca
A: 

You can not declare the variables like this. You need to do it like:

var menu="menu"; 
var container="menu_container";
var text="menu_text";

assignAttr(menu, container, text); 

function assignAttr(menu,container,text) 
{ 
 alert(menu + container + text); 
} 
Yves M.
It won't work imho because you call a function that is defined after the call.
MartyIX
@MartyIX: The order of defining functions doesn't matter as long as they are in the same scope.
casablanca
It works perfectly. try it on www.jsfiddle.net
Yves M.
Ah. Didn't know that. Sorry!
MartyIX