By SlashCrew on
I want to use the data- attribute in some of my tags ( http://html5doctor.com/html5-custom-data-attributes/ ), and I have been searching for a way to include it.
Ex:
I am adding js to my form. I use hook_form_alter and the #attached property.
$path = drupal_get_path('module', 'mymodule');
$form['#attached']['js'][] = array('data' => $path . '/js/custom_values.js','type' => 'file');How do I add a data- attribute to the tag generated? I want to use it for arguments to the js script.
Comments
I believe you want
I believe you want this:
Contact me to contract me for D7 -> D10/11 migrations.
thanks.. but
$form['#attached']['js'][] = array('data' => $path . '/js/custom_values.js','type' => 'file', '#data-arg' => 'my data');Does not work, unfortunately, that would have been a nice solution thou :-)
That's because you are trying
That's because you are trying to attach an attribute to a script - attributes are part of html elements, not scripts. Are you trying to add a new attribute to the form tag itself? If so, try this:
Or it may be this:
Contact me to contract me for D7 -> D10/11 migrations.
Every HTML element may have
http://dev.w3.org/html5/spec/Overview.html#custom-data-attribute
The script tag is an html element, so it is valid HTML5 to add the data- attribute.
But I have been doing a bit more digging, and it seems Drupal 7 does not support this functionality yet.
Checked out your blog btw. I learned a thing or two. Excellent stuff :-)
Glad to hear! Anyways, you
Glad to hear!
Anyways, you can do this, you are just doing it in the wrong spot. You left out the bit of information that you were trying to alter the <script> tag - so I wasn't able to help you until now.
You can override tags in hook_process_html_tag(&$vars). You can add your attributes in the #attributes key of the array, and it should be rendered as part of the element.
Contact me to contract me for D7 -> D10/11 migrations.
Example
This would be cool.. But it does not seem to be the way it works today
Probably you should name the data attribute to data-[module name]-city in this case, so there is not conflict between modules.
The best mode
At
You have a 'submitted' key so u can record this
The function to alter element
So at this function you have all elements to change.
Cheers,
Allcoders.
Try this
Try this Module
Form Options Attribute module