Search code examples
htmlruby-on-railsinputlink-to

How to change an input value after clicking on a link_to?


<input class="code_rabais" id="coupon_code" name="rabais" type="text">
<div class="ajout_rabais"><%= link_to "add", '#', :id => 'add_coupon' %></div>

I want to reset the input value of code_rabais to "" after the user clicked on "add". What is the simplest way to do that?


Solution

  • You can do as following:

    link_to "add", '#', id: 'add_coupon', onclick: "$('#coupon_code').val('');return false;"
    

    The other way is to add an event listener on the click event of the link_to:

    link_to "add", '#', :id => 'add_coupon'
    
    # javascript in the same view 
    $('#add_coupon').on('click', function(event) {
      $('#coupon_code').val('');
      event.preventDefault();
    })