How to dynamically keep input field empty when select box option is no?
Ì have form where writing on input field autofills other input field while user types email
$("#edit-submitted-yhteystiedot-sahkoposti").keyup(function(){
$("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value);
});
The thing is that if user does not want to receive newsletter and chooses no option for select box, the Input field with ID #ed开发者_如何学Goit-submitted-saannot-newsletter-newsletter-email-address should remain empty, even if user makes changes to default email field. ID of SELECT is #edit-submitted-saannot-haluan-uutiskirjeen
var $select = $('#edit-submitted-saannot-haluan-uutiskirjeen'),
$emailInput = $("#edit-submitted-saannot-newsletter-newsletter-email-address");
$select.change(function(){
if($select.val() == 'No')
$emailInput.val('');
}):
$emailInput.keyup(function(){
if($select.val() == 'No')
$emailInput.val('');
});
When the user changes the select to "no" reset the email field and set a custom attribute to know that you don't want automatic updates
$("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value).attr("noautoupdate", true);
Then change your code like this
$("#edit-submitted-yhteystiedot-sahkoposti").keyup(function(){
if (!$(this).attr("noautoupdate")) $("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value);
});
If the user changes the select to "yes" make the attribute false:
$("#edit-submitted-saannot-newsletter-newsletter-email-address").val(this.value).attr("noautoupdate", false);
精彩评论