0

With help from Howard E I was able to show the input from a dropdown field into another field as a placeholder. You can find that question here.

Now I would like to show the input of any type of input field into a sentence in a paragraph in that same form.

I tried this code, but unfortunately that didn’t work.

<p>Is [recent-years] is gonna be a good year?</p>

[recent-years] is the name of the cf7 input field. In this case a dropdown field. But also would love to know for textfields and radio/select inputfields.

I guess it’s possible with jQuery, but my knowledge isn’t that great.

Hopefully there’s someone that could help. Thanks!

Vasco
  • 17
  • 6

1 Answers1

0

You could try this on the form

<p>Is <span id="recent-years"></span> is gonna be a good year?</p>

And for the jQuery use this:

 jQuery(document).ready(function( $ ){
   $('select[name="recent-years"]').on('change', function(){
       updateParagraph(); 
   });
  updateParagraph();
  
  function updateParagraph(){
    // Assign variable $placeholder to the chosen value
       let $placeholder = $('select[name="recent-years"]').val();
       // replace 'your-field-name' with the cf7 field name
       $('input[name="your-field-name"]').attr('placeholder', $placeholder);
       //New code to also add to the form html
       $('#recent-years').html($placeholder);
  }
});

Tested working, see below

enter image description here

Tami
  • 686
  • 4
  • 8
  • Thanks Tami, that's almost the solution I need, but it doesn't work properly. When I add the code it shows the value underneath the sentence, and not in it as it should. So the value shows up, but strange enough at the wrong place. – Vasco Jan 18 '21 at 19:58
  • @Vasco I can fix that, already know what the issue is. will update the answer shortly. – Tami Jan 18 '21 at 22:24
  • @Vasco I'm updating the answer now with a working version. I just realized you are the same person from the years dropdown!! :) – Tami Jan 18 '21 at 22:38
  • thanks. I also saw that the first option of the dropdown don’t show up in the paragraph. I think because it’s probably the standard value. Any idea how to fix this? – Vasco Jan 18 '21 at 22:39
  • Yeah, I can try – Tami Jan 18 '21 at 22:40
  • OK, got it, updated the answer again. You move the functionality to a function, and call the function on load and on change. Cheers! – Tami Jan 18 '21 at 22:44
  • Yes it's me! ;-) Still learning... I added this code to the form interface but it does not work at my end. :-( I only added a sentence to the `$placeholder` like `$('input[name="your-field-name"]').attr('placeholder', "my-sentence " + $placeholder);`. But that shouldn't be a problem I think. – Vasco Jan 18 '21 at 23:12
  • So the last version of the code definitely works, I updated the Gif and now you can see it displays the year on load too. I also added your sentence, which shouldn't have made any problems, and it didn't. Could you clarify what is it that doesn't work for you exactly? If nothing works, probably the way you are inserting the jquery is the problem, can you check the console for errors? – Tami Jan 19 '21 at 11:47
  • I used the exact same code and placed it at the top of the form and it doesn't work. So I checked the console and it says `SyntaxError: Unexpected token '<'` which is strange in my opinion, because there's no `<` in the code. When I click on the error to see the details it shows this: `jQuery(document).ready(function( $ ){ $('select[name="recent-years"]').on('change', function(){ updateParagraph(); }); updateParagraph();

    function updateParagraph(){` Do you see the `

    ` and `

    `? There not in the code...
    – Vasco Jan 19 '21 at 20:03
  • I think the issue is: placing jquery code within the form. It looks like CF7 does not support it? You should add this jquery either through a file, of using something like `wp_add_inline_script`. Or, perhaps your theme has an option to add JS, or through one of those insert JS plugins, that I personally hate, but would solve your problem. You need to insert your jquery code in a way that the site can render it correctly. – Tami Jan 19 '21 at 20:23
  • Perhaps you are missing encapsulating the code within tags, but I am still not sure if CF7 will support adding JS directly in their form editor @Vasco – Tami Jan 19 '21 at 20:25
  • Thanks Tami, I encapsulated the code in the `` tags. And it worked before based on the other question. I could try to use a JS plugin, but as you already say, I also don't prefer that. – Vasco Jan 19 '21 at 20:44
  • So the theme I use supports to add additional JS. I added the script there and it works. But still not in the sentence: [screenshot] (https://imgur.com/a/ZvvAdXc) – Vasco Jan 19 '21 at 20:52
  • It must be your theme/site's css, as in my clean install that issue doesn't happen, as you can see in my gif. If you can share a URL for me to check, I should be able to figure it out tomorrow when I'm back at my PC. Without seeing the issue, I'm afraid I can't help. – Tami Jan 20 '21 at 00:15
  • Hi Tami, I tried several things but that didn't worked out. I use a div class for css `
    ` and when I delete this it works. So there is something interfering between the code and css. Please see the link to the [form](https://www.europaragraph.nl/online-belastingformulier-staging/)
    – Vasco Jan 24 '21 at 10:07
  • Hey @Vasco , there is no form on that link that I can see, it's just a full width, full height background image with a text and nothing else... – Tami Jan 24 '21 at 13:06
  • Hi @Tami, please try this [link](https://www.europaragraph.nl/online-belastingformulier). Thanks! – Vasco Jan 25 '21 at 20:31
  • Can you put the step of the form where the recent-years field is? That's a lot to fill in and many steps! – Tami Jan 26 '21 at 20:18
  • Hi @Tami, I deleted the style for now and when I did, the year appears perfectly in the sentence. So there's something with the style. – Vasco Feb 07 '21 at 14:31
  • Sounds like you solved it, great. Can you accept the answer when you can, please? – Tami Feb 07 '21 at 20:00
  • Thank you @Vasco – Tami Feb 08 '21 at 21:32