1

I'm trying to achieve the following:

this.inputs[options.el.find('form').attr('class')] = {};

this.inputs[options.el.find('form').attr('class')][options.elements[x].selector] = false;

However, I'm unable to do the above without a syntax error!

Any ideas how I could achieve this Object structure?

4
  • 1
    There's nothing wrong with the syntax, so I'm guessing your objects aren't structured how you think they are. Commented Jan 21, 2013 at 10:16
  • 1
    In order to answer you, I have two questions: what is the value for options.elements[x].selector? What is the Syntax error? Commented Jan 21, 2013 at 10:17
  • @AlexStack this: input[name="username"] Commented Jan 21, 2013 at 10:18
  • what is the message of the syntax error? Commented Jan 21, 2013 at 10:27

2 Answers 2

2

That syntax looks legal, but these long one liners aren't doing anyone any favors. Break it apart a bit so you can find where it's failing.

var className = options.el.find('form').attr('class');
var selector = options.elements[x].selector;

this.inputs[className] = {};
this.inputs[className][selector] = false;
Sign up to request clarification or add additional context in comments.

Comments

0

The index to an object should always be a string:

var obj = {
  a: 2;
}

obj.a = 3; //a is 3 now
obj['a'] = 4;//a is 4 now

You said options.elements[x].selector is input[name="username"] so I guess in this line:

this.inputs[options.el.find('form').attr('class')][options.elements[x].selector] = false;

What you are really trying to do is:

this.inputs[options.el.find('form').attr('class')]['username'] = false;

So you can do like this:

var sel = options.elements[x].selector;
console.log( 'sel is ' + sel );
this.inputs[options.el.find('form').attr('class')][sel] = false;

Make sure that sel is a string. You may want to try

var sel = options.elements[x].selector.value;

The .value bit retrieves the text from inside an input element.

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.