user1595858
user1595858

Reputation: 3878

properties setting issue in javascript

When I am sending the properties as name, value the name is sent as "name" instead of the argument value of name. Is there a fix for it? Right now I use case to send in right way.

Working one

/**
     * Change properties of the elements
     */
    this.changeProperties = function(type,value) {
        switch(type)
        {
        case "stroke":
            $.DrawEngine.changeProperties({"stroke":value});
            break;
        case "font-family":
            $.DrawEngine.changeProperties({"font-family":value});
            break;
        }
    };

Not working one

    this.changeProperties = function(type,value) {
            $.DrawEngine.changeProperties({"stroke":value});
}

Reason

it sends {type:"red"} instead of {"stroke": "red"}

Upvotes: 0

Views: 46

Answers (1)

PSL
PSL

Reputation: 123739

You probably need to pass it as

this.changeProperties = function(type,value) {
      var obj = {};
      obj[type] = value;
      $.DrawEngine.changeProperties(obj);
}

if you had been trying to add the object as {type:value}. key is not evaluated as type's actual value instead it becomes the key itself. So you would need to use array notation to insert the value of type being created as the key.

See this

Upvotes: 1

Related Questions