I’d like to have a field that doesn’t show at first, but if the user decided to input to it, then it will appear in the form.
I can use Jquery and do something like
$('input[example]')
.bind('click', function() {
$('#clickfield')
.append('<input id="new_field" name="MODEL[new_field]" size="30" type="text">');
However, I’d like to add this field in several different forms for different models.
Since the model name is in the field, how would I write the Jquery script to be modular so I can use the same script for all my forms?
One solution is to make a function with the names you want as input parameters:
Where
formwould be the selector of the field ('#clickfield'in the example case),idwould be the id of the new input field, andmodelNamewould be the name of the model you mentioned.Other solution, assuming
'input[example]'selects some button or clickable area that triggers the event of showing the field; and'#clickfield'is somedivorspanor some area where you will show your field, is to load these hidden fields from the server, styled asdisplay:none, with a mnemonic class, so you could do something like define a function:and then:
this way you could add some user friendly features like fading the field in with the
fadeIn()Jquery function (instead ofshow()), or any other animation on appearing.Hope you could use it! good luck!