Forums

Clean a value of text box using events

Kronosites 04 Jan, 2016
Hi!

I'm trying to clean a value from a text-box depending of the option selected in a dropdown.

One way is use events to enable or disable the text-box dending of the value selected in de dropdown, BUT is an user put manually some data in the text-box, after that change the dropdown option and the text-box is "disabled", the data inserted remains. I need to clean this data if the text-box is disable (when 1 option from the dropdown is selected).
Can I do this using "events" inside the dropdown?

I try to use this JS code too (added in SETUP -> onload event), but it didn't work:
window.addEvent('domready', function() {
  var select, other;
  select = $('dropdownID');
  other  = $('textboxID');
  switchOther();
  select.addEvent('change', switchOther);

  function switchOther() {
    if ( select.value == '1' ) {
      other.disabled = false;
    } else {
      other.value = '';
      other.disabled = true;
    }
  }
});
GreyHead 05 Jan, 2016
Hi Kronosites,

You can do this using the function option in the Events tab Actions box to call a custom JavaScript function in a Load JavaScript action in the form On Load event.

But do you really need to do this - disabled elements don't submit any values so removing the value only has a cosmetic effect?

Note: the code you posted is using the MooTools library that Joomla dropped in Joomla! 3 - you'd need to re-write in jQuery for the current version.

Bob
This topic is locked and no more replies can be posted.