1

I have a form with dynamic fields. some items contain this input fields, some don't.

Here is my code for the XML :

$field = '<field name="id_'.$filter_record['id'].'_from" type="text"
                        label="From" description="'.$filter_record['name'].'"
                         size="'.$filter_record['holdout'].'" format="%Y-%m-%d"
                        maxlength="20"  
                        required="true"                                             
                        class = "datefrom"      
                        data-holdout="7"                        
                        filter="user_utc" />

as you can see, I have a size attribute there which contains a dynamic value. Now when I want to use the data-holdout in the front-end,

using this code:

var x = jQuery(".datefrom").data("holdout");

when I alert the value of x it says undefined.

How should I add the custom attribute for it to be seen in the front page?

  • datafrom is probably incorrect. Inspect the element in your browser to get the ID from it, then use that in your jQuery – Lodder Mar 26 '15 at 8:05
2

The JFormField of Type Text does not support the data-* attribute, so your line

data-holdout="7" 

in your XML file won't have any effect on your input text HTML element, ie your input element won't have a data-holdout="7" attribute and .data("holdout"); will return undefined on that element.

With a custom JFormField you can support all attributes that you need. Here is a tutorial how to build a custom JFormField: https://docs.joomla.org/Creating_a_custom_form_field_type

By the way: JFormFieldText (which you are using through 'type="text"' in your XML) does not support the format attribute either. You should probably have a look at the calendar form field type.

  • Msg by Dean "@fruppel that's pretty standard behavior with most frameworks to assign custom attributes on form elements. It's pretty heavy for very mundane task like this to create a custom JFORM element for something simple. " – TryHarder Mar 17 '16 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.