suman
suman

Reputation: 195

adding attribute in input field of Jade with javascript variable

I want to add attribute in input field with a local javascript variable as:

-var requiredField = ('agent' == user.account_type.toLowerCase()) ? 'required' : '';

input(type='text', name="name", placeholder="Name", "#{requiredField}")

But I get the attribute as #{requiredField} in the html. How can this be achieved?

I am using jade. Thanks for help in advance

Upvotes: 2

Views: 1941

Answers (1)

Jay Harris
Jay Harris

Reputation: 10055

Jade is smart enough to render the required attribute based off of a boolean value: required=true or required=false.

   input(type='text',required=true)
   input(type='text',required=false)

will render out as:

<input type='text' required />
<input type='text' />

So for your code sample, try this instead:

-var isRequired = 'agent' == user.account_type.toLowerCase();
input(type='text', name="name", placeholder="Name", required=isRequired)

Upvotes: 2

Related Questions