Kalpesh Jain
Kalpesh Jain

Reputation: 409

Pass string as a parameter

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);
}

Upvotes: 0

Views: 149

Answers (3)

Yves M.
Yves M.

Reputation: 3318

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); 
} 

Upvotes: 0

casablanca
casablanca

Reputation: 70701

You simply need to call the function like this:

assignAttr("menu", "menu_container", "menu_text");

Upvotes: 4

MartyIX
MartyIX

Reputation: 28648

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");

Upvotes: 2

Related Questions