2

Hi I am stuck here pretty bad. The senario is quite simple actually, I have an input field like this:

 %input.coupon_bar{:type => "text", :name=> "coupon", id=>"coupon_id"}/

And I want to pass whatever value is in that box to a different controller, I am trying to do it like this:

= link_to image_tag("ok_button.png", :border =>0), "/orders/new/", :coupon = #{$('.coupon_bar').val()}

I thought it would be simple using the jquery $('.coupon_bar').val() but I guess its not as simple as I thought... any help please????

Oh, and unlike This StackOverFlow Question, my input field is not part of any form...

Thanks in advance...

Community
  • 1
  • 1
Umer Hassam
  • 1,332
  • 5
  • 23
  • 42

2 Answers2

3

I see there two bad practices:

  1. Don't hardcode your links

  2. Avoid obstrusive js when possible

Here is what I suggest:

= link_to image_tag("ok_button.png", :border =>0), new_order_path, :id => "my_link"

And in your js:

<script type="text/javascript" charset="utf-8">

$().ready(function(){
    $('#my_link').click(function(){
        $(this).attr('href', $(this).attr('href') + '?coupon=' + $('.coupon_bar').val());
    });
});

</script>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
1

Why don't you just put it in a form? That would be much easier.

Anyway, you can use the onclick from the link to achieve your goal:

<a href="#" onclick="window.location.href = '/orders/new/coupon='+ $('.coupon_bar').val();">
Text</a>
Simon Woker
  • 4,994
  • 1
  • 27
  • 41