Trip
Trip

Reputation: 27114

What is the correct syntax for writing custom attributes in a jQuery method?

I have a ternary ahead of variable instantiations. The problem is, that this is an incorrect way to assign a variable for an attribute.

$partial = $data.cell_info_box === undefined ? job_box : cell_info_box
$rel     = $($data.$partial).attr('rel');
$klass   = $($data.$partial).attr("rel").match(/job/) == null ? 'task' : 'job';

How can I provide my ternary like demonstrated but create callable attributes with my initial ternary's product?

Upvotes: 1

Views: 56

Answers (1)

rybosome
rybosome

Reputation: 5126

Based on your comment, what you want is $data[$partial]. This syntax is used when you want to get a value from an object without knowing the key name until runtime. You may also see this problem incorrectly solved through the use of eval but this is the correct way.

Upvotes: 1

Related Questions