Post by magisterson » Tue Oct 22, 2019 4:14 pm

Hey guys!
I have 3 radio option fields on product page. I need to show extra input (text field) ONLY if one of them is selected. How to do it?

I think the simplest way will be add another option position (input text) and then add in jQuery simple if/else to show this input only is specific radio is selected. It's only one option that I know. Maybe you know better one? I need this co collect ID card number to add extra TAX. If user select first radio then input text is showing and he must write there his ID card number.

New member

Posts

Joined
Tue Jul 23, 2013 10:15 pm

Post by letxobnav » Tue Oct 22, 2019 4:22 pm


Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by magisterson » Tue Oct 22, 2019 4:29 pm

I know how to write it, but I asked is there maybe other easiest way to do it in OC ;)

New member

Posts

Joined
Tue Jul 23, 2013 10:15 pm

Post by thekrotek » Tue Oct 22, 2019 4:57 pm

magisterson wrote:
Tue Oct 22, 2019 4:29 pm
I know how to write it, but I asked is there maybe other easiest way to do it in OC ;)
There's no easier way, you need to add a jQuery.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by magisterson » Wed Oct 23, 2019 7:27 am

OK, i made it in this way:

Code: Select all

<style>
.idcardnumber{
  display:none;
}
</style>

Code: Select all

<script type="text/javascript">
$(document).ready(function() {
   $('input[type="radio"]').click(function() {
       if($(this).attr('value') == '18') {
            $('.idcardnumber').show();  
       }
       else {
            $('.pesel').hide();   
       }
   });
});
</script>
But I have another problem. Those fields has to be required if they are visible. I was trying to add JS code to add REQUIRED to the input field if radio is selected, and inputs are visible. But it not working. I can set this inputs as required in admin panel, but they are required all the time, and this is not what I want because if they are not visible, and they are reuquired user can't go forward.

I can't figure out how OC makes them required... there is no JS there. no REQUIRED attribute in html code, nothing.
Any ideas how to change them to REQUIRED only if radio is selected?

New member

Posts

Joined
Tue Jul 23, 2013 10:15 pm

Post by letxobnav » Wed Oct 23, 2019 9:00 am

The required class in the html (twig) only serves to put the indicator there.
The actual validation of the input fields is done in the controller.
so in the controller you can add that if the radio selection = x and the input of the field is empty, set the error.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan
Who is online

Users browsing this forum: paulfeakins and 34 guests