Jitender
Jitender

Reputation: 7971

Missing formal parameter javascript

I am making an object. I have to assign key and value from html fields. But when I am doing that. It is giving an error "missing formal parameter". fiddle

<span class="value">
    <input type="checkbox" name="slt" value="1">
    <input type="checkbox" name="slt" value="0" checked="checked">
</span>


<select class="key">
<option value="1">one</option>
</select>


var a= {$('[name="slt"]:checked').val():$('.key').val()}
console.log(a)

Upvotes: 0

Views: 921

Answers (2)

Leo
Leo

Reputation: 13838

You thought {} is object literal, but it's not, in this case it's actually parsed as a block of code.

Nobody knows what the "object key" is, until the code is executed. Also no one knows whether it is primitive type, or can be (or should be) converted to primitive (object keys must be primitive - string).

As a block, the content is not a legal expression, and it produces error.

Upvotes: 1

Satpal
Satpal

Reputation: 133403

Use Bracket notation

//Declare object
var a= {};
//Use Bracket notation
a[$('[name="slt"]:checked').val()] = $('.key').val();

console.log(a)

DEMO

Upvotes: 2

Related Questions