The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Target span for Title section
Hello,
I'm attempting to have a drop down value appear in the title of a section. Example: Section Title - [dropdown value]
Formidable support has suggested this is doable using the following method using HTML & Javascript:
Customize HTML, under specific section:
<div id="frm_field_[id]_container" class="frm_form_field frm_section_heading form-field[error_class]">
<h3 class="frm_pos_[label_position][collapse_class]">[field_name]<span id="things-to-do-dynamic-value"></span></h3>
[if description]<div class="frm_description">[description]</div>[/if description]
[collapse_this]
</div>
In order to work I now need to target the span with Javascript and the var from the dropdown. I know how to get the value of the dropdown using something like this: var val1 = $("input[name='item_meta[2082]']:checked").val()
Can anyone help me the rest of the way to target the span id in the above HTML?
Thank you!
June 19, 2017 at 7:43 pm
In jQuery it is $('span#things-to-do-dynamic-value').text(val1);
June 20, 2017 at 12:23 am
Like this?
HTML:
<div id="frm_field_[id]_container" class="frm_form_field frm_section_heading form-field[error_class]">
<h3 class="frm_pos_[label_position][collapse_class]">[field_name] <span id="changethis"></span></h3>
[if description]<div class="frm_description">[description]</div>[/if description]
[collapse_this]
</div>
AFTER FIELDS:
<script type="text/javascript">
jQuery(document).ready(function($){
$(document).on('change', 'select[name="item_meta[243]"]', function(){
var val1 = $("select[name='item_meta[243]']").val();
$('span#changethis').text(val1);});
});
</script>
Thank you!!
June 20, 2017 at 5:59 am
Nice try, but it's not quite right. In the after fields, the jQuery should be:
jQuery(document).ready(function ($) {
"use strict";
$('#field_ihiiwi2').on('change', function(){
var val1 = $('#field_ihiiwi2').val();
$('span#changethis').text(val1);
});
});
Make sure you change the field_ihiiwi2 to the HTML element ID of your field.
In a general sense, instead of using the HTML element name ( select[name='item_meta[243]'] ), use the element's HTML ID instead.
When you add a field to a Formidable form, Formidable uses the term "ID" to describe the number that will be rendered as the HTML element name. What Formidable calls the field key will be rendered as the HTML element ID. The terminology can be a little confusing. The Formidable field IDs will change if you export/import your form into another instance. This means that any custom code you've written that references the Formidable field ID, whether jQuery (HTML element name) or PHP, will be invalid after an import. It will absolutely drive you crazy chasing down bugs to fix if that happens. The HTML element's field ID is rendered from the Formidable field key. Field keys persist even if you migrate your forms to another instance. It's a much safer coding practice to reference fields in code by the HTML element ID or Formidable field key.
June 20, 2017 at 8:19 am
haha it works! It did arrange it sorta funny. The 'change this' is now behind the Title, i.e.:
'change this' Title, instead of Title 'change this'
Thanks for all your help :-)
June 20, 2017 at 8:30 am
Well I guess I should also say, it doesn't stick when I reload the form after the form has been saved. Shoot.
June 20, 2017 at 8:49 am
When you reload the form, you're not triggering the change event. If you want the value to persist, you have to store the value in a hidden field. If the hidden field is populated, use the value from the field, otherwise use the value when the drop down changes. Something like this should work. Change field_hidden to the HTML element ID of your hidden field.
jQuery(document).ready(function ($) {
"use strict";
display_span();
$('#field_ihiiwi2').on('change', function(){
var val1 = $('#field_ihiiwi2').val();
/* populate the hidden field */
$('#field_hidden').val(val1);
display_span();
});
function display_span() {
var val1 = $('#field_hidden').val();
if ( val1 != '' ) {
$('span#changethis').text(val1);
}
}
});
July 6, 2017 at 4:12 pm
This is going really well - thank you for all your help! Any idea why the data field is being displayed prior to the title field?
It looks like in the attached. I was hoping to get 'Select' over on the far right side.
Thank you again!
Attachment:
July 7, 2017 at 4:18 am
You have the span after the title in your custom html. The fiend name shortcode is the title. If you want the span before, reverse its position in the custom html:
<h3 class="frm_pos_[label_position][collapse_class]"><span id="things-to-do-dynamic-value"></span> [field_name]</h3>
Discussion closed.