Support Forum
Support › MB Frontend Submission › Add "css class" to Element with Switch Valua after Submit Front End SubmissionResolved
Hi, I have a Frond End Submission Form only for Logged In Users.
The Form contains a Switch Field. There is a Sidebar with the TOC of the Form Fields.
After submitting the form, it is also editable afterwards by the user
I want to add a class="green" to the li-tag, if the Switch was turned ON= value 1.
That is the Form while Editing.
https://mybeverageguide.com/member-area/form-drink-recipe/?rwmb_frontend_field_post_id=17596&rwmb-form-submitted=35517c9f39362109a496e130be8920a4#editor-recipe-notes
<iframe src="https://pastebin.com/embed_iframe/HbS5H5cq" style="border:none;width:100%"></iframe>
<ul>
<li class=
{% set ShowColor = post.recipes_AddEditorsRecipesNotes_switch %}
{% if ShowColor == 1 %}
"green member-area__form-toc--mandatory "
{% else %}
"member-area__form-toc--mandatory "
{% endif %}
><a href="#editor-recipe-notes">Recipe Notes</a></li>
<ul>
Hello Timo,
>> I want to add a class="green" to the li-tag, if the Switch was turned ON= value 1.
If you use the PHP code or Twig code, the class will be added to the <li>
tag after saving the field value. If you want to add the class immediately after switching, please use the Javascript code.
Hi Peter, thanks for the answer, but unfortunately it does not work. My Twig Code is not getting the Value from the Post after I submit it.
All other things (Textinputs, and queries from the Posts are working well)
Here is a short video of the example:
https://komododecks.com/recordings/f4PdsusSoAAnTirrDiEb
And here is the updated code.
{% set ShowGreen = post.recipes_ShowGeneralInfosFP_switch %}
{% if ShowGreen == 1 %}
"green member-area__form-toc--mandatory "
{% else %}
"member-area__form-toc--mandatory "
{% endif %}
I already created a js code with session storage and it works, but I wanted to add a class via MB View.
I can give a accees to the stagging site.
Hello,
Please share the staging site by filling in the info in the contact form https://metabox.io/contact/
and let me know where I can see the issue on your site. I will take a look.
I got help from your Support and I gave further the URL MetaBox Docs to ChatGTP.
By the end I got this solution.
I put the Script and the HTML both into a Bricks Template. And on the same Template I also run the MBView Shortcode.
Now it works =)
To get the ID from the URL and utilize it with the provided explanation, you can use JavaScript to extract the ID from the URL, then pass it to the Twig template using the mb proxy.
First, add a script in your Bricks layout to get the post ID from the URL and assign it to a JavaScript variable:
<script>
function getPostIdFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('rwmb_frontend_field_post_id') || 0;
}
const postId = getPostIdFromUrl();
</script>
Next, use the JavaScript postId variable to set the value of an HTML input element, which will be used to pass the value to the Twig template:
<input type="hidden" id="postId" value="{{ postId }}">
In your Twig template, use the mb proxy to get the post ID value from the input element, then fetch the post data and display it:
{% set post_id = mb.get_field_value('postId') %}
{% set drink_recipe_query = mb.get_posts({post_type: 'drink-recipes', p: post_id, post_status: ['publish', 'draft'], posts_per_page: 1}) %}
{% for recipe in drink_recipe_query %}
<h2>{{ recipe.post_title }}</h2>
<div>{{ recipe.post_content }}</div>
{% else %}
<p>No drink recipe found with the given ID.</p>
{% endfor %}
With this approach, you can extract the post ID from the URL using JavaScript, pass it to the Twig template through an HTML input element, and use the Meta Box mb proxy to fetch and display the post data.