I have multiple tabs on the screen, each with their own identical, yet dynamically generated form. Each form has it’s own unique ID, and each field within the form has a universally unique ID.
My question pertains to using happy.js (jquery plugin) in a dynamic way. happy.js configuration uses json to set field name and the handling as an object.
I’m trying to accomplish something like the following:
$('#capture'+tabname).isHappy({
fields: {
'#first'+tabname: {
required: true,
message: 'Please tell us your full name.'
},
}
})
The ‘#first’+tabname is what is causing me problems. Is there a way to use a dynamic string there?
Yes. You’re just passing an object literal to
isHappy, so rather than creating it in-place, create it beforehand and use[]syntax to create the dynamic key: