0

i am trying to write a JavaScript function that takes the parameters and assigns parameter value to a variable...how to do this...

here is what i have tried.....

function getTextWidth(text, fontname, fontsize) () {
     var one = text;
     var two = fontname;
     var three = fontsize;
    });

is this correct?

1
  • NO need ); after your function body Commented May 22, 2013 at 11:08

6 Answers 6

3
function getTextWidth(text, fontname, fontsize) {
     var one = text;
     var two = fontname;
     var three = fontsize;
    }

You need to remove the extra brackets at the end of your function before the first opening {. Then you need to remove the trailing bracket and semi-colon. It looks like you are mixing a JavaScript function with jQuery.

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

2 Comments

It should be noted that these variables will only live within the function, so the function effectively does nothing.
true,removing my useless comment :D
1

You have incorrect syntax. See this for an example of how to declare a function with parameters:

function getTextWidth(text, fontname, fontsize) {
     var one = text;
     var two = fontname;
     var three = fontsize;
}

Comments

0

Just remove the () just before the { in your function, and replace the }); with };

Comments

0

The correct syntax is -

function getTextWidth(text, fontname, fontsize){
     var one = text;
     var two = fontname;
     var three = fontsize;
}

Comments

0

There is no need for var one = text; etc - "text" is already a variable available to your function

Comments

0

no need for extra ()

function getTextWidth(text, fontname, fontsize) {
         var one = text || false,
            two = fontname || false,
            three = fontsize || false;

        if (one) {

        }

        if (two) {

        }

        if (three) {

        }
    });

and then you can call getTextWidth('foobar', 'arial', '12px);

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.