milano
milano

Reputation: 475

How to take parameter values in javascript

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?

Upvotes: 0

Views: 107

Answers (6)

Alex
Alex

Reputation: 10216

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

Upvotes: 0

KevInSol
KevInSol

Reputation: 2610

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

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

The correct syntax is -

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

Upvotes: 0

Curtis
Curtis

Reputation: 103358

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

Upvotes: 1

Neil Knight
Neil Knight

Reputation: 48547

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.

Upvotes: 3

alexandernst
alexandernst

Reputation: 15109

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

Upvotes: 0

Related Questions